Skip to content

Commit

Permalink
fix a error when the socket connect is not blocking
Browse files Browse the repository at this point in the history
add a Decorator to checking the socket's blocking
  • Loading branch information
Falseen committed Feb 23, 2017
1 parent 72ea225 commit a2cab50
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from os import SEEK_CUR
import os
import sys
import functools
from collections import Callable
from base64 import b64encode

Expand All @@ -79,6 +80,25 @@

_orgsocket = _orig_socket = socket.socket


def set_self_blocking(function):

@functools.wraps(function)
def wrapper(*args, **kwargs):
self = args[0]
try:
_is_blocking = self.gettimeout()
if _is_blocking == 0:
self.setblocking(True)
return function(*args, **kwargs)
except Exception as e:
raise(e)
finally:
# set orgin blcoking
if _is_blocking == 0:
self.setblocking(False)
return wrapper

class ProxyError(IOError):
"""
socket_err contains original socket.error exception.
Expand Down Expand Up @@ -707,7 +727,7 @@ def _negotiate_HTTP(self, dest_addr, dest_port):
HTTP: _negotiate_HTTP
}


@set_self_blocking
def connect(self, dest_pair):
"""
Connects to the specified destination through a proxy.
Expand Down

0 comments on commit a2cab50

Please sign in to comment.