2011-02-14 59 views
1

你好 有没有什么办法通过代理使用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(("http://www.python.org",80)) 
File "<string>", line 1, in connect 
gaierror: [Errno -5] No address associated with hostname 

感谢

回答

1

您可以尝试使用SocksiPy:它会建立到代理服务器的连接,并尽一切为你工作。

0

我能解决使用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() 
+0

后的代码示例,请 – 2016-04-29 10:15:45