๊ฐ์
Dreamhack ์น ํดํน ๊ฐ์๋ฅผ ์๊ฐํ๋ ์ค Blind SQL Injection ๋ถ๋ถ์ ์ฝ๋๋ฅผ ๋ณด๊ณ ์ด๋๊น์ง ํ๋ ์ฝ๋ฉ๊ณผ๋ ๋ค๋ฅด๋ค๋ ๊ฒ์ ๋๊ปด ์ฝ๋ ๋ถ์์ ์ธ๋ถ์ ์ผ๋ก ์งํํ๊ณ Python์์ ์ฃผ๋ก ์ฌ์ฉํ๋ Class
์ self
์ ๋ํด ํฌ์คํ
ํ์๋ค.
Python self
Dreamhack ๊ฐ์ ์ค ์ ๊ณต๋ ์ฝ๋์์๋ ๋ค์๊ณผ ๊ฐ์ด ํด๋์ค๋ฅผ ์ ์ํ๊ณ self ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฝ๋ฉํ ๊ฒ์ ๋ณผ ์ ์๋ค. ๋ํ, github์ ์กด์ฌํ๋ ์ฝ๋๋ค๋ง ๋ณด๋๋ผ๊ณ self๋ฅผ ํ์ฉํ๋ ๊ฒ์ ๋ง์ด ๋ณผ ์ ์๋ค.
import requests
import sys
from urllib.parse import urljoin
class Solver:
"""Solver for simple_SQLi challenge"""
# initialization
def __init__(self, port: str) -> None:
self._chall_url = f"๋์ ์ฌ์ดํธ:{port}"
self._login_url = urljoin(self._chall_url, "login")
# base HTTP methods
self
๊ฐ ๋์ฒด ๋ญ๊น?
์ฐ์ self
๋ ๋จ์ด์ ์๋ฏธ ๊ทธ๋๋ก ๊ฐ์ฒด์ ์ธ์คํด์ค ๊ทธ ์์ฒด๋ฅผ ์๋ฏธํ๋ค.
์ผ๋ฐ์ ์ผ๋ก ํด๋์ค ๋ด๋ถ์ ์ ์๋ ํจ์์ธ ๋ฉ์๋์ ์ฒซ๋ฒ์งธ ์ธ์๋ ๋ฐ๋์ self ์ฌ์ผ ํ๋ค๋ ๋ฐฉ์์ผ๋ก ๋ง์ด ์๊ณ ์๋ค.
ํ์ง๋ง, ํด๋น ๋ง์ ํ๋ฆฐ ๋ง๋ ์๋๋ฉฐ ๋ฌด์กฐ๊ฑด ์ ์ผ๋ก ๋ง๋ ๋ง๋ ์๋๋ค. ๋ค์ ์ฝ๋๋ฅผ ํตํด ์ดํด๋ณด๋ฉด ์ฝ๊ฒ ์ดํด๊ฐ ๊ฐ๋ฅํ๋ค.
Test
๋ผ๊ณ ํ๋ ํด๋์ค๊ฐ ์กด์ฌํ๊ณ test1
, test2
๋ฉ์๋๊ฐ ์กด์ฌํ๋ค๊ณ ๊ฐ์ ํ๊ณ ํด๋์ค Test ์ ๋ํ ์ธ์คํด์ค test_instance
๋ฅผ ์์ฑํ์๋ค.
class Test:
def test1():
print("test1")
def test2(self):
print("test2")
if __name__ == "__main__":
test_instance = Test()
์ดํ test2()
ํจ์๋ฅผ ํธ์ถํ๋ฉด ์ค๋ฅ ์์ด test2
๊ฐ ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋๋ค.
if __name__ == "__main__":
test_instance = Test()
test_instance.test2() # ์ถ๊ฐ(test2() ์คํ)
ํ์ง๋ง, test1()
์ ์คํํ ๊ฒฝ์ฐ test1()์ ์ธ์๋ 0๊ฐ ์ด์ง๋ง ์ธ์๋ฅผ 1๊ฐ ๋ฐ์๋ค๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
Traceback (most recent call last):
File "/test2.py", line 9, in <module>
test_instance.test1()
TypeError: Test.test1() takes 0 positional arguments but 1 was given
์ ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊น?
Python์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฉ์๋์ ์ฒซ๋ฒ์งธ ์ธ์๋ก ํญ์ ์ธ์คํด์ค๊ฐ ์ ๋ฌ๋๊ธฐ ๋๋ฌธ์ด๋ค.
test2()๋ฅผ ์คํํ์ ๋ ์ฒซ๋ฒ์งธ ์ธ์๊ฐ self ์ด๋ฏ๋ก ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์๋ค. ํ์ง๋ง, test1()์ ๊ฒฝ์ฐ ์ธ์๊ฐ ์กด์ฌํ์ง ์์์์๋ ๋ถ๊ตฌํ๊ณ ์ธ์คํด์ค๊ฐ ์ ๋ฌ๋์ด ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค.
[Python] self ๋ ๋ฌด์์ผ๊น?
๊ฒฐ๋ก
๊ฒฐ๋ก ์ ์ผ๋ก self
๋ ๊ฐ์ฒด์ ์ธ์คํด์ค ๊ทธ ์์ฒด๋ฅผ ์๋ฏธํ๊ณ ์๊ธฐ ์์ ์ ์ฐธ์กฐํ๋ ๋งค๊ฐ๋ณ์์ด๋ค.
References


์ค๋ฅ, ์๋ชป๋ ์ ๋๋ ๊ถ๊ธํ ์ ์ด ์์ผ์๋ค๋ฉดย ๋๊ธ ๋จ๊ฒจ์ฃผ์ธ์โ
Uploaded by N2T
'๊ฐ๋ฐ๐ป > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] ํ์ด์ฌ ๋ฆฌ์คํธ(List) (1) | 2022.11.22 |
---|---|
[Python] most likely due to a circular import ์ค๋ฅ (0) | 2022.11.11 |
[Python] Argparse ๋ชจ๋ ์ฌ์ฉ๋ฒ ๋ฐ Cli ์ต์ (0) | 2022.11.11 |
[Crawling] ํฉ๋ฒ์ ์ธ Web Crawling ํ๋ ๋ฒ (0) | 2022.11.06 |
[Python] Shadow DOM ํฌ๋กค๋ง - Selenium (0) | 2022.11.01 |