我在python中编写了一个使用cookie和POST / GET的脚本。我还在脚本中包含了代理支持。但是,当一个人进入死代理代理时,脚本崩溃。在运行我的其余脚本之前,有没有办法检查代理是否死/活?
此外,我注意到一些代理不能正确处理cookie / POST头。有没有什么办法解决这一问题?
我在python中编写了一个使用cookie和POST / GET的脚本。我还在脚本中包含了代理支持。但是,当一个人进入死代理代理时,脚本崩溃。在运行我的其余脚本之前,有没有办法检查代理是否死/活?
此外,我注意到一些代理不能正确处理cookie / POST头。有没有什么办法解决这一问题?
最简单的方法是从urllib中捕获IOError异常:
try:
urllib.urlopen(
"http://example.com",
proxies={'http':'http://example.com:8080'}
)
except IOError:
print "Connection error! (Check proxy)"
else:
print "All was fine"
另外,来自 这篇博客文章 - “检查状态代理地址” (略有改进):
import urllib2
import socket
def is_bad_proxy(pip):
try:
proxy_handler = urllib2.ProxyHandler({'http': pip})
opener = urllib2.build_opener(proxy_handler)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib2.install_opener(opener)
req=urllib2.Request('http://www.example.com') # change the URL to test here
sock=urllib2.urlopen(req)
except urllib2.HTTPError, e:
print 'Error code: ', e.code
return e.code
except Exception, detail:
print "ERROR:", detail
return True
return False
def main():
socket.setdefaulttimeout(120)
# two sample proxy IPs
proxyList = ['125.76.226.9:80', '213.55.87.162:6588']
for currentProxy in proxyList:
if is_bad_proxy(currentProxy):
print "Bad Proxy %s" % (currentProxy)
else:
print "%s is working" % (currentProxy)
if __name__ == '__main__':
main()
请记住,这可能会使脚本占用的时间加倍,如果代理已关闭(因为您将不得不等待两个连接超时)。除非您特别需要知道代理有问题,否则处理IOError会更简洁,更简单更快..
我认为更好的方法就像dbr所说,处理异常。
在某些情况下可能更好的另一种解决方案是使用外部 在线代理检查器 检查代理服务器是否处于活动状态的工具,然后继续使用您的脚本而不进行任何修改。
有一个很好的包 抓 所以,如果你没问题,你可以写这样的东西(简单有效的代理检查器生成器):
from grab import Grab, GrabError
def get_valid_proxy(proxy_list): #format of items e.g. '128.2.198.188:3124'
g = Grab()
for proxy in proxy_list:
g.setup(proxy=proxy, proxy_type='http', connect_timeout=5, timeout=5)
try:
g.go('google.com')
except GrabError:
#logging.info("Test error")
pass
else:
yield proxy