易截截图软件、单文件、免安装、纯绿色、仅160KB

python 脚本 - 自动检测代理速度

这个脚本是在 python 环境下使用的,改的网上的一个脚本,可以检测代理中国(www.proxycn.com)上的HTTP代理列表,你也可以自己去上面找列表检测 代码: #!/usr/bin/python # -*- coding: utf-8 -*- # from: ubuntu.org.cn Copyright: GPLv2 import urllib import re from datetime import datetime import socket def findporxy(): url = "http://www.proxycn.com/html_proxy/http-1.html" f = urllib.urlopen(url) html = f.read() p = re.compile('clip\(\'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,4})\'\);') ips=set(p.findall(html)) socket.setdefaulttimeout(5) a={} for ip in ips: start=datetime.now() flag = checkproxy(ip) end=datetime.now() if flag: print ip,'花费',end-start,'时间'; a[end-start]=ip b=sorted(a) print '速度排序 :' i = 0 for ix in a: print 'No.',i,': ',a[b[i]] i = i + 1 def checkproxy(ip): try: proxies = {'http': 'http://'+ip} filehandle = urllib.urlopen("http://www.0x201.cn/moyo/speed_x.jpg", proxies=proxies) html=filehandle.read() if len(html)==80996: return True else: return False except: return False if __name__ == '__main__': findporxy() 下载


相关文档:

Python三目运算,and or陷阱

在C语言中,三目运算经常用到(cond?a:b),非常的简洁,而在Python中不支持这种语法。
但是,可以用Python中and or来实现(这里是有陷阱的,下面会讲到)
我们来看下面几个表达式
>>> False
or 1
1
>>> False
or 0
0
>>> True or
0
True
>>> True
and 1
1
>> ......

python exec和eval


exec语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句。下面是一个简单的例子。
>>> exec 'print "Hello World"'
Hello World
eval语句用来计算存储在字符串中的有效Python表达式。下面是一个简单的例子。
>>> ......

python中eval, exec, execfile,和compile


eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果。
同样地, exec语句将字符串str当成有效Python代码来执行.提供给exec的代码的名称空间和exec语句的名称空间相同.
最后,execfile(filename [,globals [,locals ]])函数可以用来执行一个文件,看下面的例子:
>>> ev ......

话说Python(七)从Go...ogle说起

今天一上课,大牛老师就给大家出了一道题:
编程:请从字符串“goOoOogle”中找出以“O”开头,并以“O”结束的部分。
“这还不简单,看我的”小菜不一会儿就给出了答案:
>>> s="goOoOogle"
>>> s.find("O")
2
>>> s.find("O",3)
4
>>& ......

Python的bug

Python太火了,不学点都感觉自己不是学计算机的,今天看了个不错的《简明python教程》,很不错。不过在学习过程中,居然发现了一个Python的bug,
#!/usr/bin/python
#coding=UTF-8
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person's data.'''
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号