我有这个代码,我正在尝试使用代理来访问我的主机上的链接,只是为了测试。但显然,代理不起作用。该请求仍然通过我的IP发送(如同根本没有代理)。这是代码:
import urllib2, time
filename = 'proxies.txt'
link = 'http://host.com/'
def build_proxy(proxy):
init = 'http://'
final = str(init+proxy)
return final
def proxy_list(lv=[]):
l = []
for x in range(0, len(lv)):
l.append(build_proxy(lv[x]))
return l
with open(filename, 'r') as f:
lines = ([line.rstrip() for line in f])
lenl = len(lines)
new = proxy_list(lines)
for x in range(0,lenl):
try:
proxy = urllib2.ProxyHandler({'http':str(new[x])})
headers = {'Host':'myhost.com',
'User-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0',
'Accept':'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
req = urllib2.Request(link, None, headers)
resp = urllib2.urlopen(req).read()
print resp
time.sleep(0)
except urllib2.URLError, e:
print "Error"
pass
except urllib2.HTTPError, e:
print "Error"
pass
except socket.error, e:
print "Error"
pass
except socket.gaierror, e:
print "Error"
pass
except socket.herror, e:
print "Error"
pass
有人能告诉我我做错了什么吗?我很感激一些帮助。 代理人还活着。测试。 发送请求,一切都成功(无错误),但请求不会路由到代理并返回。它只显示我的IP。
谢谢!