1,学习这部分,应先了解一下OSI 7层模型,TCP/IP四层,Socket套接字等网络相关知识点。
Python网络库,urllib,urllib2,后者功能更强大一些。
import urllib2 import urllib response = urllib2.urlopen('http://www.baidu.com') #获取百度首页的代码,打开远程文件 urllib.urlretrieve(‘http://www.baidu.com','baidu_index.html') #下载文件并保存到本地 html = response.read() print html
还有smtplib,email,cgi,httplib,ftplib,Cookie等其他模块,现用现查即可。
2,socket模块
服务器端Demo
import socket s = socket.socket() # host = socket.gethostname() port = 1234 s.bind((host,port)) s.listen(5) while True: c,addr = s.accept() print 'client link from:',addr c.send('Hello, client. This is server') c.close()
SocketServer模块,简化服务器端编程。
from SocketServer import TCPServer,StreamRequestHandler class Handler(StreamRequestHandler): def handle(self): #处理放在handle方法中 addr = self.request.getpeername() print 'get link from:',addr self.wfile.write('welcome, link succeed') server = TCPServer(('',1234),Handler) server.serve_forever() #begin to listen and handle link
#运行结果 bowen:~ bowen$ vimSocketServer_server.py bowen:~ bowen$ python SocketServer_server.py get link from: ('192.168.42.184', 49533)
客户端Demo
import socket s = socket.socket() host = socket.gethostname() port = 1234 s.connect((host,port)) #连接服务器 print s.recv(1024) s.close()
服务器与客户端通信Demo
bowen:Python bowen$ cat server.py import socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create Socket sock.bind(('localhost',8001)) #bind ip and port sock.listen(5) #start listening while True: conn,addr = sock.accept() print 'link from:',addr try: conn.settimeout(5) buff = conn.recv(1024) if buff == '1': conn.send('welcome to server') #send msg to client else: conn.send('Plead go out') except socket.timeout: print 'time out exception' conn.close() #close link bowen:Python bowen$ cat client.py import socket import time sock = socket.socket() sock.connect(('localhost',8001)) time.sleep(2) sock.send('1') print sock.recv(1024) #recv msg from server sock.close()
#运行结果 bowen:~ bowen$ vimSocketServer_server.py bowen:~ bowen$ python SocketServer_server.py get link from: ('192.168.42.184', 49533)
3,异步通信
1,分叉方式(ForkingMixIn);Server类提供了多连接处理,和数据传输服务。
from SocketServer import TCPServer,ForkingMixIn,StreamRequestHandler class Server(ForkingMixIn, TCPServer): pass class Handler(StreamRequestHandler): def handle(self): addr = self.request.getpeername() print 'get link from:',addr self.wfile.write('many links by fork') if __name__ == '__main__': server = Server(('localhost',1234),Handler) server.serve_forever()
2,线程方式(ThreadingMixIn);与分叉处理类似,区别仅在于Server继承的是ThreadingMixIN
from SocketServer import TCPServer,ThreadingMixIn,StreamRequestHandler class Server(ThreadingMixIn, TCPServer): pass class Handler(StreamRequestHandler): def handle(self): addr = self.request.getpeername() print 'get link from:',addr self.wfile.write('many links in Thread way') if __name__ == '__main__': server = Server(('localhost',1234),Handler) server.serve_forever()
3,异步I/O方式,select()方法用于对指定的文件描述符进行监视,并在描述符改变时做出响应。还有poll()方法也可实现。
import socket, select s = socket.socket() s.bind(('localhost',1234)) s.listen(5) inputs = [s] #store socket req while True: rs,ws,es = select.select(inputs,[],[]) #use select() for r in rs: if r is s: conn,addr = s.accept() print 'get link form:',addr inputs.append(conn) #put socket conn into inputs else: try: data = r.recv(1024) disconnected = not data except socket.error: disconnected = True if disconnected: print r.getpeername(),' disconnected' inputs.remove(r) else: <span style="white-space:pre"> </span> print data
时间: 2024-10-17 05:09:46