Understanding system threads through Python scripts

Source: Internet
Author: User
Tags socket thread

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.