本文整理汇总了Python中socks.SOCKS5属性的典型用法代码示例。如果您正苦于以下问题:Python socks.SOCKS5属性的具体用法?Python socks.SOCKS5怎么用?Python socks.SOCKS5使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socks
的用法示例。
在下文中一共展示了socks.SOCKS5属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update_config
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def update_config(self):
if int(self.config.PROXY_ENABLE):
if self.config.PROXY_TYPE == "HTTP":
proxy_type = socks.HTTP
elif self.config.PROXY_TYPE == "SOCKS4":
proxy_type = socks.SOCKS4
elif self.config.PROXY_TYPE == "SOCKS5":
proxy_type = socks.SOCKS5
else:
self.logger.error("proxy type %s unknown, disable proxy", self.config.PROXY_TYPE)
raise Exception()
socks.set_default_proxy(proxy_type, self.config.PROXY_HOST, self.config.PROXY_PORT,
self.config.PROXY_USER,
self.config.PROXY_PASSWD)
示例2: main
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def main():
conf = json.load(open("./proxy.conf"))
if 'gfw_proxy' in conf:
proxy = conf['gfw_proxy']
if proxy['type'] == "socks5":
proxy['type'] = socks.SOCKS5
elif proxy['type'] == "socks4":
proxy['type'] = socks.SOCKS4
else:
print "Only support SOCKS4/5 proxy."
return
print "Start TCP Proxies..."
for f in TCP_FORWARDS:
trd = TCP_Proxy_Thread(f['local_port'], f['remote_server'], proxy)
trd.start()
else:
print "gfw_proxy not defined in proxy.conf"
示例3: proxy_connect
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def proxy_connect(args):
"""
Connect to specified proxy
"""
if args.proxy_host:
# Setup proxy by monkeypatching the standard lib
if args.proxy_type.lower() == "socks5" or not args.proxy_type:
socks.set_default_proxy(
socks.SOCKS5, args.proxy_host,
int(args.proxy_port))
elif args.proxy_type.lower() == "http":
socks.set_default_proxy(
socks.HTTP, args.proxy_host,
int(args.proxy_port))
elif args.proxy_type.lower() == "socks4":
socks.set_default_proxy(
socks.SOCKS4, args.proxy_host,
int(args.proxy_port))
else:
printNicely(
magenta('Sorry, wrong proxy type specified! Aborting...'))
sys.exit()
socket.socket = socks.socksocket
示例4: load_proxy_config
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def load_proxy_config():
global default_socket
if g.config.PROXY_ENABLE:
if g.config.PROXY_TYPE == "HTTP":
proxy_type = socks.HTTP
elif g.config.PROXY_TYPE == "SOCKS4":
proxy_type = socks.SOCKS4
elif g.config.PROXY_TYPE == "SOCKS5":
proxy_type = socks.SOCKS5
else:
xlog.error("proxy type %s unknown, disable proxy", g.config.PROXY_TYPE)
raise Exception()
socks.set_default_proxy(proxy_type, g.config.PROXY_HOST, g.config.PROXY_PORT,
g.config.PROXY_USER, g.config.PROXY_PASSWD)
示例5: connect
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def connect(self) -> socket:
try:
sockt = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
sockt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if self.proxy is not None:
sockt.set_proxy(socks.SOCKS5, self.proxy["host"], self.proxy["port"])
sockt.connect((self.ip, self.port))
return sockt
except Exception as e:
print(e)
示例6: socket_SOCKS5_test
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def socket_SOCKS5_test():
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
s.connect(("ifconfig.me", 80))
s.sendall(raw_HTTP_request())
status = s.recv(2048).splitlines()[0]
assert status.startswith(b"HTTP/1.1 200")
示例7: SOCKS5_connect_timeout_test
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def SOCKS5_connect_timeout_test():
s = socks.socksocket()
s.settimeout(0.0001)
s.set_proxy(socks.SOCKS5, "8.8.8.8", 80)
try:
s.connect(("ifconfig.me", 80))
except socks.ProxyConnectionError as e:
assert str(e.socket_err) == "timed out"
示例8: SOCKS5_timeout_test
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def SOCKS5_timeout_test():
s = socks.socksocket()
s.settimeout(0.0001)
s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
try:
s.connect(("ifconfig.me", 4444))
except socks.GeneralProxyError as e:
assert str(e.socket_err) == "timed out"
示例9: socket_SOCKS5_auth_test
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def socket_SOCKS5_auth_test():
# TODO: add support for this test. Will need a better SOCKS5 server.
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081, username="a", password="b")
s.connect(("ifconfig.me", 80))
s.sendall(raw_HTTP_request())
status = s.recv(2048).splitlines()[0]
assert status.startswith(b"HTTP/1.1 200")
示例10: socket_SOCKS5_IP_test
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def socket_SOCKS5_IP_test():
s = socks.socksocket()
s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
s.connect(("133.242.129.236", 80))
s.sendall(raw_HTTP_request())
status = s.recv(2048).splitlines()[0]
assert status.startswith(b"HTTP/1.1 200")
示例11: urllib2_handler_SOCKS5_test
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def urllib2_handler_SOCKS5_test():
opener = urllib2.build_opener(sockshandler.SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 1081))
status = opener.open("http://ifconfig.me/ip").getcode()
assert status == 200
示例12: global_override_SOCKS5_test
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def global_override_SOCKS5_test():
default_proxy = (socks.SOCKS5, "127.0.0.1", 1081)
socks.set_default_proxy(*default_proxy)
good = socket.socket
socket.socket = socks.socksocket
status = urllib2.urlopen("http://ifconfig.me/ip").getcode()
socket.socket = good
assert status == 200
assert socks.get_default_proxy()[1].decode() == default_proxy[1]
示例13: _open_proxied_socket
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def _open_proxied_socket(url, options, proxy):
hostname, port, resource, is_secure = parse_url(url)
if not HAS_PYSOCKS:
raise WebSocketException("PySocks module not found.")
ptype = socks.SOCKS5
rdns = False
if proxy.type == "socks4":
ptype = socks.SOCKS4
if proxy.type == "http":
ptype = socks.HTTP
if proxy.type[-1] == "h":
rdns = True
sock = socks.create_connection(
(hostname, port),
proxy_type = ptype,
proxy_addr = proxy.host,
proxy_port = proxy.port,
proxy_rdns = rdns,
proxy_username = proxy.auth[0] if proxy.auth else None,
proxy_password = proxy.auth[1] if proxy.auth else None,
timeout = options.timeout,
socket_options = DEFAULT_SOCKET_OPTION + options.sockopt
)
if is_secure:
if HAVE_SSL:
sock = _ssl_socket(sock, options.sslopt, hostname)
else:
raise WebSocketException("SSL not available.")
return sock, (hostname, port, resource)
示例14: parse_proxy_str
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def parse_proxy_str(proxy_str):
"""
Returns proxy from given string
"""
if socks is None:
raise Exception('Please install PySocks if you want to use a proxy')
url_parser = urlparse(proxy_str)
proxy_type = None
proxy_type_str = url_parser.scheme
if proxy_type_str.lower() == "socks5":
proxy_type = socks.SOCKS5
elif proxy_type_str.lower() == "socks4":
proxy_type = socks.SOCKS4
elif proxy_type_str.lower() == "https":
proxy_type = socks.HTTP
elif proxy_type_str.lower() == "http":
proxy_type = socks.HTTP
else:
raise ValueError("Proxy type %s is not supported" % proxy_type)
host = url_parser.hostname
port = url_parser.port
if host is None:
raise ValueError("Host parsing error")
if port is None:
raise ValueError("Port parsing error")
user = url_parser.username
password = url_parser.password
if user is not None and password is not None:
proxy = (proxy_type, host, port, True, user, password)
else:
proxy = (proxy_type, host, port)
return proxy
示例15: test_parse_proxy_str
# 需要导入模块: import socks [as 别名]
# 或者: from socks import SOCKS5 [as 别名]
def test_parse_proxy_str(self):
host = "127.0.0.1"
port = 1080
proxy = (socks.SOCKS5, host, port)
proxy_str = "socks5://127.0.0.1:1080"
self.assertEqual(parse_proxy_str(proxy_str), proxy)
proxy_str = "http://127.0.0.1:1080"
proxy = (socks.HTTP, host, port)
self.assertEqual(parse_proxy_str(proxy_str), proxy)
proxy_str = "socks4://login:password@127.0.0.1:1080"
proxy = (socks.SOCKS4, host, port, True, "login", "password")
self.assertEqual(parse_proxy_str(proxy_str), proxy)
proxy_str = "bad_type://login:password@127.0.0.1:1080"
with self.assertRaises(ValueError):
parse_proxy_str(proxy_str)
proxy_str = "bad_type://127.0.0.1"
with self.assertRaises(ValueError):
parse_proxy_str(proxy_str)
proxy_str = "bad_type:127.0.0.1"
with self.assertRaises(ValueError):
parse_proxy_str(proxy_str)
proxy_str = "127.0.0.1:1080"
with self.assertRaises(ValueError):
parse_proxy_str(proxy_str)