Python socket network programming, pythonsocket
Socket: to allow two different applications to communicate.
Two socket types: file-based and network-based
1. file-based (AF_LOCAL or AF_UNIX): Unix
2. network-oriented (AF_INET): representing the Internet
Connection-oriented socket (SOCK_STREAM): TCP
For connectionless Sockets (SOCK_DGRAM): UDP
Network Programming socket () module in Python
TCP network program
Serice Server
#! /Usr/bin/env python #-*-coding: UTF-8-*-from socket import * from time import ctimeHOST = ''# null indicates bind () the function can be bound to all valid addresses. PORT = 21567 BUFSIZ = 1024 # buffer size 1 KADDR = (HOST, PORT) tcpSerSock = socket (AF_INET, SOCK_STREAM) tcpSerSock. bind (ADDR) tcpSerSock. listen (5) # the maximum number of connections allowed will be denied later. while True: print 'Waiting for connection... 'tcpclisock, addr = tcpSerSock. accept () # blocking thread print '... connected from: ', addr while True: data = tcpCliSock. recv (BUFSIZ)
# Blocked threads
if not data: break tcpCliSock.send('(%s) %s' % (ctime(),data)) tcpCliSock.close() 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 # buffer size 1 K 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 break17 tcpCliSock. send (data) 18 data = tcpCliSock. recv (BUFSIZ) 19 if not data: 20 break21 print data22 tcpCliSock. close ()
UDP Program
Service Server
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 = 102410 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:',addr20 udpSerSock.close()
View Code
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 break17 udpCliSock.sendto(data,ADDR)18 data,ADDR = udpCliSock.recvfrom(BUFSIZ)19 if not data:20 break21 print data22 23 udpCliSock.close()
View Code
SocketService communication program
Server
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_address14 self.wfile.write('[%s] %s' % (ctime(),self.rfile.readline()))15 16 tcpServ = TCP(ADDR,MyRequestHandler)17 print 'waiting for connection...'18 tcpServ.serve_forever()
View Code
Client
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 from socket import * 5 HOST = 'localhost' ## 6 # HOST = '123. 168.0.51 '7 PORT = 21569 8 BUFSIZ = 1024 # buffer size 1 K 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 break17 tcpCliSock. send ('% s \ r \ n' % data) 18 data = tcpCliSock. recv (BUFSIZ) 19 if not data: 20 break21 print data. strip () 22 tcpCliSock. close ()
View Code