Basic threaded Python TCP server,threadedpython,TCP provides
Basic threaded Python TCP server,threadedpython,TCP provides
TCP provides reliable, ordered delivery of a stream of bytes from a program onone computer to another program on another computer. Sometimes you need aquick deployment of a TCP server and here I bring to you a Python 2.* snippetof a threaded Python server.
from socket import *import threadBUFF = 1024HOST = '127.0.0.1'# must be input parameter @TODOPORT = 9999 # must be input parameter @TODOdef gen_response(): return 'this_is_the_return_from_the_server'def handler(clientsock,addr): while 1: data = clientsock.recv(BUFF) print 'data:' + repr(data) if not data: break clientsock.send(gen_response()) print 'sent:' + repr(gen_response()) clientsock.close()if __name__=='__main__': ADDR = (HOST, PORT) serversock = socket(AF_INET, SOCK_STREAM) serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) serversock.bind(ADDR) serversock.listen(5) while 1: print 'waiting for connection...' clientsock, addr = serversock.accept() print '...connected from:', addr thread.start_new_thread(handler, (clientsock, addr))
相关内容
- python的按钮单击双击事件处理,python按钮,from Tkinter
- python的魔法__slots__,python魔法__slots_,看python社区大妈组
- python列出文件夹下的所有文件或者目录,,python的os模块
- python使用 glob 获得文件列表,pythonglob,import osos.
- python base64 decode出现incorrect padding错误的解决方法,ba
- 用Buildout来构建Python项目,buildout构建python,什么是Buil
- python获得windows的进程信息,pythonwindows进程,import win32
- 使用pip命令查看系统环境中已经安装的python包,pippyt
- python下载大文件代码,,如果下载小文件,可以直接
- python文本比较库difflib使用示例,pythondifflib,python的包很
评论关闭