Python network programming (eight)

Source: Internet
Author: User

Single-Process server 1. Complete a Simple TCP server
 fromSocketImport*Sersocket=socket (af_inet, sock_stream)#reusing information for bindingsSersocket.setsockopt (Sol_socket, SO_REUSEADDR, 1) Localaddr= ("', 7788) Sersocket.bind (localaddr) Sersocket.listen (5) whileTrue:Print('-----The main process, wait for the new client to arrive------') newsocket,destaddr=sersocket.accept ()Print('-----The main process, next responsible for data processing [%s]-----'%str (DESTADDR))Try:         whileTrue:recvdata= NEWSOCKET.RECV (1024)            ifLen (RecvData) >0:Print('recv[%s]:%s'%(str (DESTADDR), recvdata))Else:                Print('[%s] client is closed'%str (DESTADDR)) Break    finally: Newsocket.close () sersocket.close ()
2. Summary
    • Can only serve one customer at a time, not at the same time for multiple customer service
    • Similar to looking for a "star" signature, customers need to wait patiently to get to the service
    • 当服务器为一个客户端服务时,而另外的客户端发起了connect,只要服务器listen的队列有空闲的位置,就会为这个新客户端进行连接,并且客户端可以发送数据,但当服务器为这个新客户端服务时,可能一次性把所有数据接收完毕
    • When recv receives data, the return value is empty, that is, no data is returned, which means that the client has called close closed, so the server determines whether the client is offline by judging whether the recv receives the data is empty
Multi-Process server 1. Multi-Process Server
 fromSocketImport* fromMultiprocessingImport* fromTimeImportSleep#process client requests and service themdefdealwithclient (newsocket,destaddr): whileTrue:recvdata= NEWSOCKET.RECV (1024)        ifLen (RecvData) >0:Print('recv[%s]:%s'%(str (DESTADDR), recvdata))Else:            Print('[%s] client is closed'%str (DESTADDR)) Breaknewsocket.close ()defMain (): Sersocket=socket (af_inet, Sock_stream) sersocket.setsockopt (Sol_socket, SO_REUSEADDR,1) Localaddr= ("', 7788) Sersocket.bind (localaddr) Sersocket.listen (5)    Try:         whileTrue:Print('-----The main process, wait for the new client to arrive------') newsocket,destaddr=sersocket.accept ()Print('-----The main process, next create a new process responsible for data processing [%s]-----'%str (DESTADDR)) client= Process (Target=dealwithclient, args=(NEWSOCKET,DESTADDR)) Client.start ()#because a copy (reference) has been copied to the child process, and the socket in the parent process is useless.            #so Closenewsocket.close ()finally:        #closed after all client services have been completed, indicating no longer receiving links from new clientssersocket.close ()if __name__=='__main__': Main ()
2. Summary
    • Ability to service multiple clients at the same time by creating a process for each client
    • This is fine when the client is not particularly large, and if there are hundreds of thousand, it is not available because the process requires a good resource each time the process is created.
Multi-Threaded Server
#Coding=utf-8 fromSocketImport* fromThreadingImportThread fromTimeImportSleep#handle client requests and do thingsdefdealwithclient (newsocket,destaddr): whileTrue:recvdata= NEWSOCKET.RECV (1024)        ifLen (RecvData) >0:Print('recv[%s]:%s'%(str (DESTADDR), recvdata))Else:            Print('[%s] client is closed'%str (DESTADDR)) Breaknewsocket.close ()defMain (): Sersocket=socket (af_inet, Sock_stream) sersocket.setsockopt (Sol_socket, SO_REUSEADDR,1) Localaddr= ("', 7788) Sersocket.bind (localaddr) Sersocket.listen (5)    Try:         whileTrue:Print('-----The main process, wait for the new client to arrive------') newsocket,destaddr=sersocket.accept ()Print('-----The main process, next create a new process responsible for data processing [%s]-----'%str (DESTADDR)) client= Thread (Target=dealwithclient, args=(NEWSOCKET,DESTADDR)) Client.start ()#Because this socket is shared in the thread, if it is turned off it will cause the socket to be unavailable.            #However, this socket may still be receiving data in the thread and cannot be closed            #newsocket.close ()    finally: Sersocket.close ()if __name__=='__main__': Main ()

 

Python network programming (eight)

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.