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('	网络测试中...%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()
原文地址:https://www.cnblogs.com/kuihua/p/6092154.html