所以我在 youtube 視頻的幫助下撰寫了一個 Socks5 服務器。有一件事我不明白,希望能得到幫助。
def run(self, host, port):
#gives it specifications, Family ip and TCP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
#ip and port to listen to
s.listen()
print("* Socks5 proxy server is running on {}:{}".format(host, port))
while True:
#3 way handshake when someone connects - TCP
conn, addr = s.accept()
#tupple of ip and port
print("* new connection from {}".format(addr))
#calling the handle_client function: handle_client(conn),conn is a socket type
print(conn)
t = threading.Thread(target=self.handle_client, args=(conn,))
t.start()
def handle_client(self, connection):
version, nmethods = connection.recv(2)
# get available methods [0, 1, 2]
methods = self.get_available_methods(nmethods, connection)
print(methods)
所以我在這段代碼中不明白的是這一行:version, nmethods = connection.recv(2). 版本是襪子的版本是5。但是我不明白的方法。什么方法?它怎么知道它有多少方法,它甚至對什么東西有方法????需要幫助,謝謝!
uj5u.com熱心網友回復:
connection.recv(2)表示接收(最多)兩個位元組(source)。因此,回傳了兩個位元組,您將設定version為第一個位元組nmethods的值和第二個位元組的值。
這是一個例子:
a = b'hi' # a is a bytes object
b, c = a # b and c are ints
print(b, c)
# output: 104 105
所以,至于什么version和nmethods意思——這只是我認為這個程式特有的東西。不過,我對套接字并不是很熟悉。我猜這nmethods意味著“方法的數量”,考慮到它會在get_available_methods()以后使用。至于它如何知道方法的數量,無論這些方法是什么,您只需要查看發送該資料的代碼即可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/488795.html
