我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用socks.PROXY_TYPE_SOCKS5。
def makesock(self): if self.config["ipv6"]: family = socket.AF_INET6 else: family = socket.AF_INET if self.proxy is not None: proxy_serv, proxy_port = self.proxy.split(":") self.sock = socks.socket(family, socket.SOCK_STREAM) self.sock.setblocking(0) self.sock.setproxy(socks.PROXY_TYPE_SOCKS5, proxy_serv, proxy_port) else: self.sock = socket.socket(family, socket.SOCK_STREAM) self.sock.settimeout(self.timeout) if self.vhost is not None: self.sock.bind((self.vhost, 0)) if self.config["ssl"]: self.sock = ssl.wrap_socket(self.sock)
def _get_proxy(self): url_dissected = url_dissector.findall( self.session['proxy'] ) if url_dissected and len(url_dissected[0]) == 3: protocol, host, port = url_dissected[0] if protocol == 'socks5': return (socks.PROXY_TYPE_SOCKS5, host, int(port)) if protocol == 'socks4': return (socks.PROXY_TYPE_SOCKS4, host, int(port)) if protocol.startswith('http'): return (socks.PROXY_TYPE_HTTP, host, int(port)) return None, None, None
def get_proxy(): proxy_server = None proxy_type_id = 0 proxy_port = 8080 proxy_user = None proxy_pass = None try: proxy_server = __settings__.getSetting('proxy_server') proxy_type_id = __settings__.getSetting('proxy_type') proxy_port = int(__settings__.getSetting('proxy_port')) proxy_user = __settings__.getSetting('proxy_user') proxy_pass = __settings__.getSetting('proxy_pass') except: pass if proxy_type_id == '0': proxy_type = socks.PROXY_TYPE_HTTP_NO_TUNNEL elif proxy_type_id == '1': proxy_type = socks.PROXY_TYPE_HTTP elif proxy_type_id == '2': proxy_type = socks.PROXY_TYPE_SOCKS4 elif proxy_type_id == '3': proxy_type = socks.PROXY_TYPE_SOCKS5 proxy_dns = True return (proxy_type, proxy_server, proxy_port, proxy_dns, proxy_user, proxy_pass)
def connect_to_server(self): self.channels_to_join = copy.deepcopy(self.irc_channels) glados.log('Connecting to: {}:{}'.format(self.host, self.port)) try: self.socket = socks.socksocket() if not self.irc_settings.setdefault('proxy host', 'none') == 'none' \ and not self.irc_settings.setdefault('proxy port', 'none') == 'none': self.socket.setproxy(socks.PROXY_TYPE_SOCKS5, self.irc_settings['proxy host'], int(self.irc_settings['proxy port']), True) self.socket.connect((self.host, self.port)) self.send_raw_message('USER {0} {0} {0} :{0}\n'.format(self.botnick)) if not self.irc_settings.setdefault('password', 'none') == '': #self.send_raw_message('PRIVMSG NickServ :IDENTIFY {} {}\n'.format(self.botnick, self.irc_settings['password'])) self.send_raw_message('PASS {}\n'.format(self.irc_settings['password'])) self.send_raw_message('NICK {}\n'.format(self.botnick)) self.state = self.STATE_TRY_JOIN except Exception as e: glados.log('Exception caught: {}'.format(e)) exc_info = sys.exc_info() traceback.print_exception(*exc_info)
def __init__(self, proxy_url, username=None, password=None, num_pools=10, headers=None, **connection_pool_kw): parsed = parse_url(proxy_url) if parsed.scheme == 'socks5': socks_version = socks.PROXY_TYPE_SOCKS5 elif parsed.scheme == 'socks4': socks_version = socks.PROXY_TYPE_SOCKS4 else: raise ValueError( "Unable to determine SOCKS version from %s" % proxy_url ) self.proxy_url = proxy_url socks_options = { 'socks_version': socks_version, 'proxy_host': parsed.host, 'proxy_port': parsed.port, 'username': username, 'password': password, } connection_pool_kw['_socks_options'] = socks_options super(SOCKSProxyManager, self).__init__( num_pools, headers, **connection_pool_kw ) self.pool_classes_by_scheme = SOCKSProxyManager.pool_classes_by_scheme
def __init__(self, proxy_url, username=None, password=None, num_pools=10, headers=None, **connection_pool_kw): parsed = parse_url(proxy_url) if parsed.scheme == 'socks5': socks_version = socks.PROXY_TYPE_SOCKS5 rdns = False elif parsed.scheme == 'socks5h': socks_version = socks.PROXY_TYPE_SOCKS5 rdns = True elif parsed.scheme == 'socks4': socks_version = socks.PROXY_TYPE_SOCKS4 rdns = False elif parsed.scheme == 'socks4a': socks_version = socks.PROXY_TYPE_SOCKS4 rdns = True else: raise ValueError( "Unable to determine SOCKS version from %s" % proxy_url ) self.proxy_url = proxy_url socks_options = { 'socks_version': socks_version, 'proxy_host': parsed.host, 'proxy_port': parsed.port, 'username': username, 'password': password, 'rdns': rdns } connection_pool_kw['_socks_options'] = socks_options super(SOCKSProxyManager, self).__init__( num_pools, headers, **connection_pool_kw ) self.pool_classes_by_scheme = SOCKSProxyManager.pool_classes_by_scheme
def run(self): while self.running: while self.running: try: if self.tor: self.socks.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) self.socks.connect((self.host, self.port)) print term.BOL+term.UP+term.CLEAR_EOL+"Connected to host..."+ term.NORMAL break except Exception, e: if e.args[0] == 106 or e.args[0] == 60: break print term.BOL+term.UP+term.CLEAR_EOL+"Error connecting to host..."+ term.NORMAL time.sleep(1) continue while self.running: try: self._send_http_post() except Exception, e: if e.args[0] == 32 or e.args[0] == 104: print term.BOL+term.UP+term.CLEAR_EOL+"Thread broken, restarting..."+ term.NORMAL self.socks = socks.socksocket() break time.sleep(0.1) pass