标签:
from socket import * from time import ctime HOST = ‘‘ PORT = 21567 BUFSIZE = 1024 ADDR = (HOST,PORT) tcpSerSock = socket(AF_INET,SOCK_STREAM) tcpSerSock.bind( ADDR) tcpSerSock.listen(5) while True: print ‘waiting for connection......‘ tcpCliSock,addr = tcpSerSock.accept( ) print ‘......connected from:‘,addr while True: data = tcpCliSock.recv(BUFSIZ) if not data: break tcpCliSock.send(‘[%s]%s‘ %(ctime(),data)) tcpCliSock.close( ) tcpSerSock.close()
1 from socket import * 2 3 HOST = ‘localhost‘ 4 PORT = 21567 5 BUFSIZ = 1024 6 ADDR = (HOST,PORT) 7 8 tcpCliSock = socket(AF_INET,SOCK_STREAM) 9 tcpCliSock.connect(ADDR) 10 11 while True: 12 data = raw_input(‘>‘) 13 if not data: 14 break 15 tcpCliSock.send(data) 16 data = tcpCliSock.recv(BUFSIZ) 17 if not data: 18 break 19 print data 20 21 tcpCliSock.close()
1 from socket import * 2 from time import ctime 3 4 HOST = ‘‘ 5 PORT = 21567 6 BUFSIZ = 1024 7 ADDR = (HOST,PORT) 8 9 udpSerSock = socket(AF_INET,SOCK_DGRAM) 10 udpSerSock.bind(ADDR) 11 12 while True: 13 print ‘waiting for message......‘ 14 data,addr = udpSerSock.recvfrom(BUFSIZ) 15 udpSerSock.sendto(‘[%s] %s‘ % (ctime,data) ,addr) 16 print ‘......received from and returned to:‘,addr 17 18 udpSerSock.close()
1 from socket import * 2 3 HOST = ‘localhost‘ 4 PORT = "21567" 5 BUFSIZ = 1024 6 ADDR = (HOST,PORT) 7 8 udpCliSock = socket(AF_INET,SOCK_DGRAM) 9 10 while Trus: 11 data = raw_input(‘>‘) 12 if not data: 13 break 14 udpCliSock.sendto(data,ADDR) 15 data,ADDR = udpCliSock.recvfrom(BUFSIZ) 16 if not data: 17 break 18 print data 19 20 udpCliSock.close()
类 | 描述 |
BaseServer | 包含服务器核心功能与混合(mix-in)类挂钩;这个类只用于派生,所以不会生成这个类的实例;可以考虑使用TCPServer和UDPServer |
TCPServer/UDPServer | 基本的网络同步TCP/UDP服务器 |
UnixStreamServer/UnixDatagramServer | 基本的基于文件同步TCP/UDP服务器 |
ForkingMixIn/ThreadingMixIn | 实现了核心的进程化或线程化的功能;作为混合类,与服务器类一并使用以提供一些异步特性;这个类不会直接实例化。 |
ThreadingTCPServer/ThreadingUDPServer | ThreadingMixIn和TCPServer/UDPServer的组合 |
BaseRequestHandler | 包含处理服务器请求的核心功能,这个类只用于派生,所以不会生成这个类的实例可以考虑使用StreamRequestHandler 或 DatagramRequestHandler |
StreamRequestHandler/DatagramRequestHandler | 用于TCP/UDP服务器处理工具 |
from SocketServer import (TCPServer as TCP,StreamRequsetHandler as SRH) from time import ctime HOST = ‘‘ PORT = 21567 ADDR = (HOST,PORT) class MyRequestHandler(SRH): def handle(self): print ‘......connected from:‘,self.client_address self.wfile.write(‘[%s]%s‘ % (ctime(),self.rfile.readline())) tcpServ = TCP(ADDR,MyRequestHandler) print ‘waiting for connection...‘ tcpServ.serve_forever( )
1 from socket import * 2 3 HOST = ‘localhost‘ 4 PORT = 21567 5 BUFSIZ = 1024 6 ADDR = (HOST,PORT) 7 8 while True: 9 tcpCliSock = socket(AF_INET,SOCK_STREAM) 10 tcpCliSock.connect(ADDR) 11 data = raw_input(‘>‘) 12 if not data: 13 break 14 tcpCliSock.send(‘%s\r\n‘ %data) 15 data = tcpCliSock.recv(BUFSIZ) 16 if not data: 17 break 18 print data.strip() 19 tcpCliSock.close() 20
1 from socket import * 2 from time import ctime 3 4 5 HOST = ‘‘ 6 PORT = 21567 7 BUFSIZ = 1024 8 ADDR = (HOST,PORT) 9 10 11 tcpSerSock = socket(AF_INET,SOCK_STREAM) 12 tcpSerSock.bind(ADDR) 13 tcpSerSock.listen(5) 14 15 16 while True: 17 print ‘waitiing for connection...‘ 18 tcpCliSock,addr = tcpSerSock.accept() 19 print ‘...connected from:‘,addr 20 21 22 while True: 23 data = tcpCliSock.recv(BUFSIZ) 24 if not data: 25 break 26 tcpCliSock.send(‘[%s] %s‘ % (ctime(),data)) 27 28 tcpSerSock.close() 29 30
1 from socket import * 2 import time,os 3 4 5 HOST = ‘‘ 6 PORT = 21567 7 BUFSIZ = 1024 8 ADDR = (HOST,PORT) 9 10 11 tcpSerSock = socket(AF_INET,SOCK_STREAM) 12 tcpSerSock.bind(ADDR) 13 tcpSerSock.listen(5) 14 15 16 while True: 17 print ‘waitiing for connection...‘ 18 tcpCliSock,addr = tcpSerSock.accept() 19 print ‘...connected from:‘,addr 20 21 while True: 22 data = tcpCliSock.recv(BUFSIZ) 23 data = data.strip() 24 if not data: 25 break 26 if data == "date": 27 tcpCliSock.send(‘%s‘ % (time.ctime(time.time()))) 28 elif data == "os": 29 tcpCliSock.send(‘%s‘ % (os.name)) 30 elif data == "ls": 31 tcpCliSock.send(‘%s‘ % (os.listdir(os.curdir))) 32 else: 33 tcpCliSock.send(‘[%s] %s‘ % (time.ctime(),data)) 34 35 36 tcpSerSock.close()
1 from socket import * 2 from time import ctime 3 4 5 HOST = "localhost" 6 7 PORT = getservbyname("daytime","udp") 8 9 BUFSIZ = 1024 10 11 ADDR = (HOST,PORT) 12 13 udpSerSock = socket(AF_INET,SOCK_DGRAM) 14 15 udpSerSock.bind(ADDR) 16 17 while True: 18 print "waiting for message..." 19 data,addr = udpSerSock.recvfrom(BUFSIZ) 20 udpSerSock.sendto("[%s]%s" % (ctime(), data),addr) 21 print "...received from and returned to:",addr 22 23 24 udpSerSock.close 25
1 from socket import * 2 from time import ctime 3 4 5 HOST = "localhost" 6 PORT = getservbyname("daytime","udp") 7 8 BUFSIZ = 1024 9 10 ADDR = (HOST,PORT) 11 12 udpCliSock = socket(AF_INET,SOCK_DGRAM) 13 14 15 while True: 16 data = raw_input(">") 17 if not data: 18 break 19 udpCliSock.sendto(data,ADDR) 20 data,ADDR = udpCliSock.recvfrom(BUFSIZ) 21 if not data: 22 break 23 print data 24 25 tcpCliSock.close()
from socket import * HOST = "localhost" PORT = 23346 BUFSIZ = 1024 ADDR = (HOST,PORT) tcpCliSock = socket(AF_INET,SOCK_STREAM) tcpCliSock.connect(ADDR) while True: data = raw_input("client>") if not data: continue tcpCliSock.send(data) while True: data = tcpCliSock.recv(BUFSIZ) if not data: continue else: print "server>",data break tcpCliSock.close()
1 from socket import * 2 3 HOST = "localhost" 4 PORT = 23346 5 BUFSIZ = 1024 6 ADDR = (HOST,PORT) 7 8 9 tcpServSock = socket(AF_INET,SOCK_STREAM) 10 tcpServSock.bind(ADDR) 11 tcpServSock.listen(5) 12 13 14 15 while True: 16 print "waiting for connection..." 17 tcpCliSock,addr = tcpServSock.accept() 18 print "connected from:",addr 19 20 while True: 21 data = tcpCliSock.recv(BUFSIZ) 22 if not data: 23 continue 24 print "client>",data 25 data = raw_input("server>") 26 tcpCliSock.send(data) 27 28 tcpServSock.close() 29
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 import socket,traceback,os 4 from threading import * 5 6 host = ‘localhost‘ 7 port = 51423 #监听所有的接口 8 9 #接受消息的线程 10 def handlerecv(clientsock): 11 print "New child",currentThread().getName() 12 print "Got connection from",clientsock.getpeername() 13 while True: 14 data = clientsock.recv(4096) 15 if not len(data): 16 break 17 print data 18 clientsock.close() 19 20 #发送消息的线程 21 def handlesend(clientsock): 22 while True: 23 data = raw_input(">") 24 data = data + "\n"; #加上换行,好看一点。 25 clientsock.sendall(data) 26 #关闭连接 27 clientsock.close() 28 29 #建立套接字 30 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 31 s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) 32 s.bind((host,port)) 33 s.listen(1) 34 35 while True: 36 try: 37 clientsock,clientaddr = s.accept() 38 except KeyboardInterrupt: 39 raise 40 except: 41 traceback.print_exc() 42 continue 43 44 t = Thread(target = handlerecv,args=[clientsock]) 45 t.setDaemon(1) 46 t.start() 47 48 r = Thread(target = handlesend,args=[clientsock]) 49 r.setDaemon(1) 50 r.start() 51 52 53 54
1 #!/usr/bin/env python 2 #-*-coding:utf-8-*- 3 4 from socket import * 5 import sys 6 from threading import * 7 8 if(len(sys.argv) < 3): 9 HOST = ‘localhost‘ 10 PORT = 51423 11 else: 12 HOST = sys.argv[1] 13 PORT = int(sys.argv[2]) 14 15 BUFSIZ = 1024 16 ADDR = (HOST,PORT) 17 18 def handlesend(tcpCliSock): 19 while True: 20 sdata = raw_input(‘> ‘) 21 if not sdata: 22 break 23 tcpCliSock.send(sdata) 24 25 tcpCliSock.close() 26 27 tcpCliSock = socket(AF_INET,SOCK_STREAM) 28 tcpCliSock.connect(ADDR) 29 30 #建立发送消息的线程 31 s = Thread(target = handlesend,args=[tcpCliSock]) 32 s.setDaemon(1) 33 s.start() 34 35 while True: 36 rdata = tcpCliSock.recv(BUFSIZ) 37 if not rdata: 38 break 39 print rdata 40 tcpCliSock.close()
标签:
原文地址:http://www.cnblogs.com/elliottc/p/4909233.html