网络编程的专利权应该属于Unix,各个平台(如windows、Linux等)、各门语言(C、C++、Python、Java等)所实现的符合自身特性的语法都大同小异。在我看来,懂得了Unix的socket网络编程,其他的形式的网络编程方法也就知道了。这句话说得还不太严谨。准确的应该说成懂得了socket编程的原理,网络编程也就知道了,不同之处就在于每个平台,每个语言都有自己专享的语法,我们直接灵活套用就行了。
下面是用python实现的最基本的网络编程的例子,即依托于客户端-服务器的架构,实现客户端与服务器之间的单向“数据流通”。我们分别用两个方法来实现,一个方法是最原始的socket编程,另一个方法是利用python的面向对象对第一种方法进行封装实现,目的是减少实现透明性,便于快速开发。
要求:客户端输入数据,发送到服务端,服务器端生成(时间戳+数据)的封装数据回应客户端。由于socket编程包括两种:面向连接的和无连接的,这两种分别对应TCP数据流和UDP数据报文。所以,我们两种方法都进行实现。
一、Python socket编程
面向连接的TCP socket编程:
1 # -*- coding: utf-8 -*- 2 3 from socket import * 4 from time import ctime 5 6 # Address and Port 7 HOST = ‘‘ 8 PORT = 21567 9 ADDR = (HOST, PORT) 10 11 # BuffSize 12 BUFSIZ = 1024 13 14 # build socket 15 tcpSerSock = socket(AF_INET, SOCK_STREAM) 16 # bind socket 17 tcpSerSock.bind(ADDR) 18 # listen 5 client 19 tcpSerSock.listen(5) 20 21 try: 22 while True: 23 print ‘waiting for connection...‘ 24 # build client socket 25 tcpCliSock, addr = tcpSerSock.accept() 26 print ‘...connect from:‘, addr 27 28 # accept data and process 29 while True: 30 data = tcpCliSock.recv(BUFSIZ) 31 if not data: 32 break 33 tcpCliSock.send(‘[%s] %s‘ % (ctime(), data)) 34 35 # close client socket 36 tcpCliSock.close() 37 except EOFError, KeyboardInterrupt: 38 tcpSerSock.close()
1 # -*- coding:utf-8 -*- 2 3 from socket import * 4 5 # Address and Port 6 HOST = ‘127.0.0.1‘ 7 PORT = 21567 8 ADDR = (HOST, PORT) 9 10 # BufferSize 11 BUFSIZ = 1024 12 13 #build socket 14 tcpCliSocket = socket(AF_INET, SOCK_STREAM) 15 tcpCliSocket.connect(ADDR) 16 17 while True: 18 data = raw_input(‘> ‘) 19 if not data: 20 break 21 # send data 22 tcpCliSocket.send(data) 23 # recv data 24 data = tcpCliSocket.recv(BUFSIZ) 25 if not data: 26 break 27 # show data 28 print data 29 tcpCliSocket.close()
无连接的UDP socket编程
1 # -*- coding: utf-8 -*- 2 3 from socket import * 4 from time import ctime 5 6 # Address and Port 7 HOST = ‘‘ 8 PORT = 8000 9 ADDR = (HOST, PORT) 10 11 # BufferSize 12 BUFFSIZE = 1024 13 # build socket 14 udpSerSock = socket(AF_INET, SOCK_DGRAM) 15 # bind socket 16 udpSerSock.bind(ADDR) 17 18 try: 19 while True: 20 print ‘waiting the message...‘ 21 data, addr = udpSerSock.recvfrom(BUFFSIZE) 22 print ‘received the message: ‘+data+‘ from: ‘, addr 23 udpSerSock.sendto(‘[%s] %s‘ % (ctime(), data), addr) 24 except EOFError, KeyboardInterrupt: 25 udpSerSock.close()
1 # -*- coding: utf-8 -*- 2 3 from socket import * 4 5 # Address and Port 6 HOST = ‘localhost‘ 7 PORT = 8000 8 ADDR = (HOST, PORT) 9 10 # BufferSize 11 BUFSIZ = 1024 12 13 # build socket 14 udpCliSock = socket(AF_INET, SOCK_DGRAM) 15 16 while True: 17 data = raw_input(‘> ‘) 18 udpCliSock.sendto(data, ADDR) 19 data = udpCliSock.recvfrom(BUFSIZ) 20 if not data: 21 break 22 print data 23 udpCliSock.close()
二、基于封装类SocketServer的网络编程
1 # -*- coding: utf-8 -*- 2 3 from SocketServer import TCPServer as TCP, StreamRequestHandler as SRH 4 from time import ctime 5 6 # Address and Port 7 HOST = ‘‘ 8 PORT = 21567 9 ADDR = (HOST, PORT) 10 11 # BuffSize 12 BUFSIZ = 1024 13 14 # build RequestHandler 15 class MyRequestHandler(SRH): 16 def handle(self): 17 print ‘...connected from: ‘, self.client_address 18 self.wfile.write(‘[%s] %s‘ % (ctime(), self.rfile.readline())) 19 20 # build TCPServer 21 TCPServ = TCP(ADDR, MyRequestHandler) 22 print ‘waiting for connection...‘ 23 # loop to process 24 TCPServ.serve_forever()
1 # -*- coding:utf-8 -*- 2 3 from socket import * 4 5 # Address and Port 6 HOST = ‘127.0.0.1‘ 7 PORT = 21567 8 ADDR = (HOST, PORT) 9 10 # BufferSize 11 BUFSIZ = 1024 12 13 while True: 14 # note: SocketServer 的请求处理器的默认行为是接受连接, 15 # 得到请求,然后就关闭连接,所以需要多次连接 16 tcpCliSock = socket(AF_INET, SOCK_STREAM) 17 tcpCliSock.connect(ADDR) 18 19 # process data 20 data = raw_input(‘> ‘) 21 if not data: 22 break 23 tcpCliSock.send(‘%s\r\n‘ % data) 24 25 data = tcpCliSock.recv(BUFSIZ) 26 if not data: 27 break 28 print data.strip() 29 tcpCliSock.close()
时间: 2024-10-08 22:16:01