Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中通过代理的套接字_Python - Fatal编程技术网

python中通过代理的套接字

python中通过代理的套接字,python,Python,你好 在python中是否有任何方法可以使用套接字通过代理进行连接。 这给了我一个错误 import socket, sys s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("www.python.org", 80)) Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> soc.connect

你好 在python中是否有任何方法可以使用套接字通过代理进行连接。 这给了我一个错误

import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("www.python.org", 80))

Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
soc.connect(("http://www.python.org",80))
File "<string>", line 1, in connect
gaierror: [Errno -5] No address associated with hostname
导入套接字,sys s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s、 连接(((“www.python.org”,80)) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 soc.连接(“http://www.python.org",80)) 文件“”,第1行,在connect中 gaierror:[Errno-5]没有与主机名关联的地址
谢谢,您可以尝试使用:它将与您的代理服务器建立连接,并为您完成所有工作。

我可以使用
urllib2
解决此问题,如下所示:

import urllib2 

opener = urllib2.build_opener(
     urllib2.ProxyHandler({"http":"proxy_ip_address:port_number";}),
     urllib2.ProxyHandler({"https":"proxy_ip_address:port_number";}),
)
urllib2.install_opener(opener) 

for line in urllib2.urlopen("py4inf.com/code/romeo.txt"): 
    print line.strip()