代理检查python [英] Proxy Check in python

查看:86
本文介绍了代理检查python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中编写了一个使用cookie和POST / GET的脚本。我还在脚本中包含了代理支持。但是,当一个人进入死代理代理时,脚本崩溃。有没有办法在运行我的其余脚本之前检查代理是否死/活?

I have written a script in python that uses cookies and POST/GET. I also included proxy support in my script. However, when one enters a dead proxy proxy, the script crashes. Is there any way to check if a proxy is dead/alive before running the rest of my script?

此外,我注意到有些代理不处理cookie / POST标题正确。有没有办法解决这个问题?

Furthermore, I noticed that some proxies don't handle cookies/POST headers properly. Is there any way to fix this?

推荐答案

最简单的方法就是从urllib中捕获IOError异常:

The simplest was is to simply catch the IOError exception from urllib:

try:
    urllib.urlopen(
        "http://example.com",
        proxies={'http':'http://example.com:8080'}
    )
except IOError:
    print "Connection error! (Check proxy)"
else:
    print "All was fine"

此外,来自此博客文章 - 检查状态代理地址(略有改进):

Also, from this blog post - "check status proxy address" (with some slight improvements):

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()

请记住,如果代理停机,这可能会使脚本占用时间翻倍(因为您将拥有等待两个连接超时)..除非y你必须要知道代理有问题,处理IOError更清晰,更简单,更快..

Remember this could double the time the script takes, if the proxy is down (as you will have to wait for two connection-timeouts).. Unless you specifically have to know the proxy is at fault, handling the IOError is far cleaner, simpler and quicker..

这篇关于代理检查python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆