Python,检查代理是否还活着?

发布于 2024-12-01 05:54:27 字数 1259 浏览 0 评论 0原文

代码:

 for item in pxfile.readlines():
     if is_OK(item):
         sys.stdout.write(item + "is not OK.")
         item = make(item)
         item = "#" + item
         resfile.write(item)
     else:
         sys.stdout.write(item)
         sys.stdout.write("is OK.")
         line = make(item)
         resfile.write(item)

如果 is_OK 为 true,则意味着代理不存在,应该修复该问题。

def is_OK(ip):
    try:
        proxy_handler = urllib2.ProxyHandler({'http': ip})
        opener = urllib2.build_opener(proxy_handler)
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        urllib2.install_opener(opener)
        req=urllib2.Request('http://www.icanhazip.com')
        sock=urllib2.urlopen(req)
    except urllib2.HTTPError, e:
        #print 'Error code: ', e.code
        return e.code
    except Exception, detail:

        #print "ERROR:", detail
        return 1
    return 0

需要 10 分钟才能得到这样的列表:

141.219.252.132:68664
is OK.118.174.0.155:8080
is OK.91.194.246.169:8080
is not OK.91.194.246.81:8080
is OK.201.245.110.138:8888
is OK.202.43.178.31:3128
is OK.202.109.80.106:8080
  1. 有没有办法让它更快?
  2. 它的格式很糟糕,我尝试用 strip() 删除换行符 但没有运气。

有什么想法吗?

The code:

 for item in pxfile.readlines():
     if is_OK(item):
         sys.stdout.write(item + "is not OK.")
         item = make(item)
         item = "#" + item
         resfile.write(item)
     else:
         sys.stdout.write(item)
         sys.stdout.write("is OK.")
         line = make(item)
         resfile.write(item)

If is_OK is true it means that the proxy doesn't exist, should fix that.

def is_OK(ip):
    try:
        proxy_handler = urllib2.ProxyHandler({'http': ip})
        opener = urllib2.build_opener(proxy_handler)
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        urllib2.install_opener(opener)
        req=urllib2.Request('http://www.icanhazip.com')
        sock=urllib2.urlopen(req)
    except urllib2.HTTPError, e:
        #print 'Error code: ', e.code
        return e.code
    except Exception, detail:

        #print "ERROR:", detail
        return 1
    return 0

It takes 10 minutes to get a list like this:

141.219.252.132:68664
is OK.118.174.0.155:8080
is OK.91.194.246.169:8080
is not OK.91.194.246.81:8080
is OK.201.245.110.138:8888
is OK.202.43.178.31:3128
is OK.202.109.80.106:8080
  1. Is there a way to make it faster?
  2. It's formatted badly, I tried removing the newline with strip()
    but no luck.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘴硬脾气大 2024-12-08 05:54:27

您应该使用线程来使代码运行得更快:

import urllib2, threading

def is_OK(ip):
    print 'Trying %s ...' % ip
    try:
        proxy_handler = urllib2.ProxyHandler({'http': ip})
        opener = urllib2.build_opener(proxy_handler)
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        urllib2.install_opener(opener)
        req=urllib2.Request('http://www.icanhazip.com')
        urllib2.urlopen(req)
        print '%s is OK' % ip
    except urllib2.HTTPError:
        print '%s is not OK' % ip
    except Exception:
        print '%s is not OK' % ip

a = threading.Thread(None, is_OK, None, ("hostname1",), None)
a.start()
b = threading.Thread(None, is_OK, None, ("hostname2",), None)
b.start()

You should use threads to make the code run quicker :

import urllib2, threading

def is_OK(ip):
    print 'Trying %s ...' % ip
    try:
        proxy_handler = urllib2.ProxyHandler({'http': ip})
        opener = urllib2.build_opener(proxy_handler)
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        urllib2.install_opener(opener)
        req=urllib2.Request('http://www.icanhazip.com')
        urllib2.urlopen(req)
        print '%s is OK' % ip
    except urllib2.HTTPError:
        print '%s is not OK' % ip
    except Exception:
        print '%s is not OK' % ip

a = threading.Thread(None, is_OK, None, ("hostname1",), None)
a.start()
b = threading.Thread(None, is_OK, None, ("hostname2",), None)
b.start()
北恋 2024-12-08 05:54:27

第一个想法,设置比默认超时更短的超时。

timeout = 10
sock=urllib2.urlopen(req, None, timeout)

您还可以使用线程,以便可以同时测试多个连接。

First idea, set a shorter timeout than default one

timeout = 10
sock=urllib2.urlopen(req, None, timeout)

You may also use threading so you can test several connections simultaneously.

月牙弯弯 2024-12-08 05:54:27

对于格式化,使用 strip() 应该可以:

for line in pxfile:
    item = line.strip()
    if is_OK(item):
        sys.stdout.write(item + " is not OK.\n")
        resfile.write("# " + make(item) +"\n")
     else:
        sys.stdout.write(item + " is OK.\n")
        resfile.write(make(item) +"\n")

And for formatting, using strip() that way should be ok:

for line in pxfile:
    item = line.strip()
    if is_OK(item):
        sys.stdout.write(item + " is not OK.\n")
        resfile.write("# " + make(item) +"\n")
     else:
        sys.stdout.write(item + " is OK.\n")
        resfile.write(make(item) +"\n")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文