From socket import * host= ' localhost ' #定义主机
Addr= (Host,port) #定义元组
Tcpsersock.listen (1) #监听用户请求
def client (tcpclisock,num): #定义函数. Because thread is forced to call a function that must have two parameters. Num is just sucks, no special meaning
Clisock=tcpclisock #将传递进来的客户端socket赋值给CliSock
While True: #定义无限循环
DATA=CLISOCK.RECV (Bufsiz) #从用户socket中读取1024大小数据
If not data: # #处理用户请求
print ' con from: ', addr <span style= ' Color:rgb (44, 44, 44); Font-family: Song body,; Background-color:rgb (255, 255, 255); "=" "28px;=" "14px;=" "serif;=" "arial,=" "narrow", = "arial=" "> #打印客户端信息
Thread.start_new_thread (client, (tcpclisock,1)) #在Python中使用线程就靠这句了! Python will start a new thread to run the client function and pass it to the function argument in tuple form!
Tcpsersock.close () #这句依然不会执行!
Threading is an official recommendation to write a thread to use the module. It is still necessary to understand thread, not only to let us know more about the operation of the system thread, but also to find the difference between thread and threading and queue, and then better learn to master threading!
Here is the Python client program. If you can use Telnet for testing, the following can be ignored.
From socket Import *
host= ' localhost '
port=21567
bufsiz=1024
Addr= (Host,port)
Tcpclisock=socket (Af_inet,sock_stream)
Tcpclisock.connect (ADDR)
While True:
Data=raw_input (">")
If not data:
Break
Tcpclisock.send (data)
DATA=TCPCLISOCK.RECV (Bufsiz)
If not data:
Break
Print data
Tcpclisock.close ()
This article is from the "Lincoln" blog, please be sure to keep this source http://president.blog.51cto.com/4990508/1081542