csonezp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

ulipad,看着蛮舒服的。

图里的代码就是今天晚上的成果。

突然就想看看python这个被很多人说是优雅的语言,于是晚上没事就配了配环境,做了个东西

 1 #encoding: utf-8
 2 import urllib
 3 import urllib2
 4 
 5 #name:要注册的名称
 6 #判断name是否可用
 7 def testUsername(name):
 8     url="http://passport.hupu.com/index.php"
 9     params={'m':'user','a':'isUsername','username':name}
10     data1=urllib.urlencode(params)
11     req=urllib2.Request(url,data1)
12     response=urllib2.urlopen(req)
13     res=response.read()
14     status=eval(res)['code']
15     if status=='1':
16         return True
17     return False
18 
19 #path:文件名 
20 #读取文件中的名字,返回文件中未注册的名称
21 def showUseableName(path):
22     
23     list_name=[]
24     file=open(path)
25     for line in file:
26         line=line.strip('\n')
27         line=line.decode('gbk').encode('utf8')
28         if testUsername(line):
29             list_name.append(line)
30     return list_name
31 
32 
33 items=showUseableName('list.txt')
34 for item in items:
35     print item.decode('utf8').encode('gbk')
36 raw_input()

会读取list.txt中的用户名去检测是否注册,如果没有注册就会显示出来。

python优雅不优雅没感觉到,但是写着蛮舒服的

posted on 2014-12-05 21:30  csonezp  阅读(320)  评论(0编辑  收藏  举报