[英]How to set SOCKS proxy to websocket-client
I'm trying to set a SOCKS proxy to the websocket-client
's WebSocket
with create_connection
but It always keeps closing the socket after websocket sends the handshake request.我正在尝试使用
create_connection
为websocket-client
的WebSocket
设置一个 SOCKS 代理,但它总是在 websocket 发送握手请求后一直关闭套接字。
Traceback (most recent call last):
File "D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_core.py", line 252, in connect
self.handshake_response = handshake(self.sock, *addrs, **options)
File "D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_handshake.py", line 79, in handshake
status, resp = _get_resp_headers(sock)
File "D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_handshake.py", line 162, in _get_resp_headers
status, resp_headers, status_message = read_headers(sock)
File "D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_http.py", line 308, in read_headers
line = recv_line(sock)
File "D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_socket.py", line 134, in recv_line
c = recv(sock, 1)
File "D:\PyCharmProjects\x-python\venv\lib\site-packages\websocket\_socket.py", line 125, in recv
raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
python-BaseException
Process finished with exit code -1
And that's how I'm trying to wrap the socket with the PySocks.这就是我试图用 PySocks 包装套接字的方式。
from socks import socksocket
proxied_sock = socksocket()
proxied_sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
proxied_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
proxied_sock = self.__proxy.set_to_socket(proxied_sock)
proxied_sock.connect((dealer_str, 80))
self.__logger.debug("WS wrapped with proxy!")
self.__ws = create_connection(self.__dealer_uri, socket=proxied_sock)
The set_to_socket()
function is just a function to wrap the proxy. set_to_socket()
function 只是一个 function 来包装代理。
def set_to_socket(self, s) -> socksocket:
"""
Sets socket's proxy
:param s: socket.socket
:return: Proxied socket.socket
"""
if self.proxy_type == "http" or self.proxy_type == "https":
s.set_proxy(HTTP, self.host, self.port, username=self.username, password=self.password)
elif self.proxy_type == "socks4":
s.set_proxy(SOCKS4, self.host, self.port, username=self.username, password=self.password)
elif self.proxy_type == "socks5":
s.set_proxy(SOCKS5, self.host, self.port, username=self.username, password=self.password)
else:
raise ProxyError("Proxy type not found: " + self.proxy_type)
return s
Why is that happening and how can I wrap my websocket with SOCKS proxy?为什么会发生这种情况,如何使用 SOCKS 代理包装我的 websocket?
Edit: I was trying to connect to an SSL secured port so I had to wrap the socket with SSL wrapper.编辑:我试图连接到一个 SSL 安全端口,所以我不得不用 SSL 包装器包装插座。
proxied_sock = socksocket()
proxied_sock = self.__proxy.set_to_socket(proxied_sock)
dealer_tuple = (dealer_str.split(":")[0], int(dealer_str.split(":")[1]))
proxied_sock.connect(dealer_tuple)
proxied_sock = ssl.wrap_socket(proxied_sock) # We gotta wrap the socket with SSL
self.__logger.debug("Dealer client wrapped with proxy!")
self.__ws = create_connection(self.__dealer_uri, socket=proxied_sock)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.