在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
谢谢。
2 个回答
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()
1
你可以试试 SocksiPy:它会帮你连接到你的代理服务器,自动处理所有的事情。