我无法通过 SOCKS5 代理发送 UDP 数据包。我正在使用 PySocks。这是我的代码:
import socks
proxyIP = "whatever.proxy"
proxyPort = 8080
s = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM)
s.set_proxy(socks.SOCKS5, proxyIP, proxyPort)
a = s.sendto(b"GET / HTTP/1.1", ("example.com",80))
在我的测试中,目的地是我的 nginx Web 服务器。代理不是我的,我只知道是 socks5 代理。这是我得到的错误:
Traceback (most recent call last):
File "test.py", line 35, in <module>
a = s.sendto(b"GET / HTTP/1.1", ("example.com",80))
File "C:\Python38\lib\site-packages\socks.py", line 367, in sendto
self.bind(("", 0))
File "C:\Python38\lib\site-packages\socks.py", line 353, in bind
_, relay = self._SOCKS5_request(self._proxyconn, UDP_ASSOCIATE, dst)
File "C:\Python38\lib\site-packages\socks.py", line 524, in _SOCKS5_request
resp = self._readall(reader, 3)
File "C:\Python38\lib\site-packages\socks.py", line 278, in _readall
raise GeneralProxyError("Connection closed unexpectedly")
socks.GeneralProxyError: Connection closed unexpectedly
我也测试过s.connect/s.sendall
,我有完全相同的错误。
最后,我必须说它与 TCP (SOCK_STREAM) 完美配合。