Understanding system processes through Python scripts

Source: Internet
Author: User

From socket Import *

From time import CTime

Import Os,sys #sys定义了进程退出的方法

host= ' localhost ' #定义主机

port=21567# defines the port number

bufsiz=1024# defines a buffer

Addr= (Host,port) #定义元组

Tcpsersock=socket (af_inet,sock_stream) #生成TCP socket

Tcpsersock.bind (ADDR) #将该Socket与元组进行绑定

Tcpsersock.listen (5) #监听请求

While True: #定义无限循环 (more and more like Shell)

Print "Waiting ..."

try:# has destroyed Tcpsersock in the build process, if not caught this exception will cause the program error!

Tcpclisock,addr=tcpsersock.accept ()

Except: #捕获所有异常

Sys.exit (0) #只要捕获到阻塞失败将退出当前进程. (only generated processes can cause blocking failures, and tcpsersock words are destroyed in the build process)

If Os.fork (): #如果在主进程中的操作

Pass

else: #如果在派生进程中的操作

print ' con from: ', addr

While True:

DATA=TCPCLISOCK.RECV (Bufsiz)

If not data:

Tcpsersock.close () #销毁tcpSerSock

break# jump out of the loop!

Tcpclisock.send (' [%s]%s '% (CTime (), data)

Tcpsersock.close () #这句永远不会被执行!

This article is from the "Lincoln" blog, please be sure to keep this source http://president.blog.51cto.com/4990508/1078402

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.