socket套接字:为了使2个不同应用程序进行通信。
两种套接字类型:基于文件的和基于网络的
1.基于文件(AF_LOCAL或AF_UNIX):代表Unix
2.面向网络(AF_INET):代表因特网
面向连接的套接字(SOCK_STREAM):TCP
面向无连接的套接字(SOCK_DGRAM):UDP
Python中的网络编程socket()模块
TCP 网络程序
serice服务端
1 # -*- coding: utf-8 -*- 2 from socket import * 3 from time import ctime 4 5 HOST = '' ##为空表示bind()函数可以绑定在所有有效的地址上 6 PORT=21567 7 BUFSIZ = 1024 ##缓冲区大小 1K 8 ADDR = (HOST,PORT) 9 10 tcpSerSock = socket(AF_INET,SOCK_STREAM) 11 tcpSerSock.bind(ADDR) 12 tcpSerSock.listen(5) ##允许最多多少个链接 后来的会被拒绝掉 13 14 while True: 15 print 'waiting for connection...' 16 tcpCliSock,addr = tcpSerSock.accept() ##阻塞式线程 17 print '...connected from:',addr 18 19 while True: 20 data = tcpCliSock.recv(BUFSIZ) 21 if not data: 22 break 23 tcpCliSock.send('(%s) %s' % (ctime(),data)) 24 tcpCliSock.close() 25 tcpSerSock.close()
client 客户端
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 from socket import * 5 HOST = 'localhost' ## 6 PORT=21517 7 BUFSIZ = 1024 ##缓冲区大小 1K 8 ADDR = (HOST,PORT) 9 10 tcpCliSock = socket(AF_INET,SOCK_STREAM) 11 tcpCliSock.connect(ADDR) 12 13 while True: 14 data = raw_input('> ') 15 if not data: 16 break 17 tcpCliSock.send(data) 18 data = tcpCliSock.recv(BUFSIZ) 19 if not data: 20 break 21 print data 22 tcpCliSock.close()
UDP程序
service 服务端
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 from socket import * 5 from time import ctime 6 7 HOST='' 8 PORT=21567 9 BUFSIZ = 1024 10 ADDR = (HOST,PORT) 11 12 udpSerSock = socket(AF_INET,SOCK_DGRAM) 13 udpSerSock.bind(ADDR) 14 15 while True: 16 print 'waiting for message....' 17 data , addr = udpSerSock.recvfrom(BUFSIZ) 18 udpSerSock.sendto('[%s] %s' % (ctime(),data),addr) 19 print '...received from and returned to:',addr 20 udpSerSock.close()
client 客户端
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 from socket import * 5 6 HOST='localhost' 7 PORT=21567 8 BUFSIZ=1024 9 ADDR = (HOST,PORT) 10 11 udpCliSock= socket(AF_INET,SOCK_DGRAM) 12 13 while True: 14 data = raw_input('> ') 15 if not data: 16 break 17 udpCliSock.sendto(data,ADDR) 18 data,ADDR = udpCliSock.recvfrom(BUFSIZ) 19 if not data: 20 break 21 print data 22 23 udpCliSock.close()
SocketService 通讯程序
服务端
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 from SocketServer import (TCPServer as TCP,StreamRequestHandler as SRH) 5 from time import ctime 6 7 HOST = '' 8 PORT = 21569 9 ADDR = (HOST,PORT) 10 11 class MyRequestHandler(SRH): 12 def handle(self): 13 print '...connected from:',self.client_address 14 self.wfile.write('[%s] %s' % (ctime(),self.rfile.readline())) 15 16 tcpServ = TCP(ADDR,MyRequestHandler) 17 print 'waiting for connection...' 18 tcpServ.serve_forever()
客户端
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 from socket import * 5 HOST = 'localhost' ## 6 # HOST = '192.168.0.51' 7 PORT=21569 8 BUFSIZ = 1024 ##缓冲区大小 1K 9 ADDR = (HOST,PORT) 10 11 while True: 12 tcpCliSock = socket(AF_INET, SOCK_STREAM) 13 tcpCliSock.connect(ADDR) 14 data = raw_input('> ') 15 if not data: 16 break 17 tcpCliSock.send('%s ' % data) 18 data = tcpCliSock.recv(BUFSIZ) 19 if not data: 20 break 21 print data.strip() 22 tcpCliSock.close()