用python建立socket服务器
给朋友写了个脚本,用到了socket服务器,翻到之前写的代码,拿到这里记录一下。
直接贴代码:
#!/usr/bin/env python
from SocketServer import ThreadingTCPServer as TCP
from SocketServer import StreamRequestHandler as SRH
from time import ctime
host = ''
port = 12345
addr = (host, port)
class MyRequestHandler(SRH):
def handle(self):
while True:
input = self.rfile.readline()
if not input.strip():
break
self.wfile.write('input content is : %s' % input)
tcpServ = TCP(addr, MyRequestHandler)
tcpServ.serve_forever()
代码仅实现了最基本的socket服务器端接收请求。