天天看點

Python網絡連接配接

1 import appuifw as ui
 2 import httplib
 3 from os import abort
 4 uia=ui.app
 5 uin=ui.note
 6 uiq=ui.query
 7 e32=ui.e32
 8 socket=httplib.socket
 9 def cn(x):return x.decode('utf8')
10 uia.body=txt=ui.Text()
11 global dic
12 dic={'p':''}
13 run=1
14 
15 def ou(url,ret=1):
16   #socket.access_point(dic['p']).start()
17   con=httplib.HTTPConnection('10.0.0.172',80)
18   con.putrequest('GET',url)
19   con.putheader('Accept','*/*')
20   con.endheaders()
21   if ret:
22     r=con.getresponse()
23     data=cn(r.read())
24     con.close()
25     return data
26   else:
27     con.close()
28     return 
29 
30 def quit():
31   if uiq(cn("是否退出程式?"),"query"):
32     if lock:
33       lock.signal()
34     else:
35       abort()
36 name=uia.full_name().split('\\')[-1][:-4].lower()
37 if name in ['ped','appmgr']:
38   lock=e32.Ao_lock()
39 else:
40   lock=None
41 def stop():
42   global run
43   run=0
44   uia.title=cn('已經停止!')
45 
46 
47 def start():
48   run=1
49   global run
50   a1=0
51   while run:
52     a1+=1
53 
54     uia.title=cn('第%s次'%a1)
55     try:
56       da=ou('http://3g.qq.cn')
57       txt.set(da[200:400])
58       uia.title=cn('第%s次'%a1)
59       da=ou('http://wap.ucweb.com')
60       txt.set(da[200:400])
61     except: pass
62     e32.ao_sleep(2)
63 
64 
65 uia.exit_key_handler=quit
66 def main():
67   uia.menu=[(cn('開始'),start),(cn('停止'),stop),(cn('退出'),quit)]
68 
69   if lock:
70     lock.wait()
71 if 1:
72   uin(cn("請選擇CMWAP接入點"))
73   dic['p']=socket.select_access_point()
74   txt.set(cn('\t網絡測試中...%s'%str(dic['p']).encode('hex')))
75   try:
76     ou('http://3gsoft.5wap.net')
77     uin(cn('網絡測試成功'))
78   except:
79     uin(cn('網絡測試失敗'))
80 txt.set('')
81 main()      
上一篇: Python ZIP壓縮
下一篇: 分組