Python Network Programming

Source: Internet
Author: User

@1: Synchronous network programming (i.e. blocking mode)

Synchronous network programming can only connect one client at a time.

Server side:

ImportSocketdefdebugprint (name, value):Print("{0}: {1}". Format (name, value))defserver ():#1:Server =Socket.socket ()#note:not "host = Server.gethostname ()"Host =socket.gethostname () port= 8080#2:    #Note:not "Server.bind (host, Port)" Typeerror:bind () takes exactly one argument (2 given)Server.bind ((host, port))#3:Server.listen (3)#Listen () parameter is the number of connections that are allowed to wait     while1:        #4:        Client, address = server.accept ()#the Accept () will block until a client connectsDebugPrint ("Client", client) DebugPrint ("Address", address)#5:        Client. Send ("welcome!") Client.close ()defMain (): Server ()

Client side:

ImportSocketImportServerdefclient ():#1:Client =Socket.socket () host=socket.gethostname () port= 8080#2:Client.connect ((host, port))#3:Content = CLIENT.RECV (1024) Server.debugprint ("From Server", content)defMain (): Client ()

@2: Asynchronous network programming (i.e. non-blocking mode)

Asynchronous network programming, allowing multiple client links.

There are 3 implementations of asynchronous network programming: Fork (multi-process), multi-threading, asynchronous IO

Fork mode occupies the resources, Windows does not support bifurcation, multi-threaded mode has synchronization problems;

Python Network Programming

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.