python--oriented network programming-socket principle

Source: Internet
Author: User

Socket

The entire computer network is made up of protocols, such as the HTTP protocol in the Web, the Transport protocol TCP and UDP, and so on, that you want to communicate with. In the eyes of the network engineer, probably now everything on the network is a socket, everything is socket, we generally touch the application layer application, essentially two applications want to communicate, you must communicate through the socket, the socket directly and the transport layer below the underlying network protocol to deal with ( The socket itself allows us to deal directly with TCP, and the underlying sockets are built to communicate with each other. Internet now the mainstream Network layer protocol is Ipv4,ipv6 is the next Generation Network layer protocol but not mainstream, IPv6 solve is IPv4 address depletion problem, in fact, in order to deal with the problem of IPV4 less resources generated LAN and gateway.

Network model

Its development process is an iterative process to solve the demand. When the computer has just been invented and put into use, the two computers want to achieve point-to-point communication, resulting in a data link layer, when adding more computers to achieve communication, the network layer is generated, the communication can not meet the demand, the need to transmit data through the network, the transmission layer is generated, The demand for reliability has generated TCP and UDP two transmission protocols, different users have different needs, so the application layer is divided. Users in the application layer to use a variety of apps, the data packets down the packet until the physical layer sent to the network, receive data and then unpacking to get the final data. In essence, the demand drives the generation of the network hierarchy, and the conversation, representation and application layer in the Network seven-layer model are referred to as the application layer at present.

Application layer File transfer, File service, email HTTP FTP SMTP DNS
Transport layer via end-to-end interface TCP UDP
Network layer Select route IP ICMP for packet
The data Link layer transmits an address frame, error detection function arp
Physical Layer Physical media

North Gate blowing Snow: http://www.cnblogs.com/2bjiujiu/

  

Generally based on the socket communication applications are using the C/S model, here to clarify the C/S architecture

Client:

Need to declare the protocol, request a connection, send and receive data, close the connection process

  

Service side:

Affirm the Protocol instance listener (source three-body), bind listening IP and port, initiate listener listener, establish connection, receive and send data, disconnect these processes

# This is a single-threaded service-side listener instance process

    

Client instance Implementation process

Import Socketdef beimenchuixue_client ():    client = Socket.socket (socket.af_inet, socket. SOCK_STREAM)    client.connect ((' 127.0.0.1 ', 8000)) while    True:        data = input ("Input English Character:")        if not Data.strip ():            continue        client.send (Data.encode (' Utf-8 '))        response = CLIENT.RECV (1024x768)        print ( Response.decode (' Utf-8 ')) if __name__ = = ' __main__ ':    beimenchuixue_client ()

  

Single thread client listener instance implementation process

Import Socketdef Beimenchuixue_alone_socket ():    listener = socket.socket (socket.af_inet, socket. SOCK_STREAM)    listener.bind ((' 0.0.0.0 ', 8000))    Listener.listen ()    while True:        conn, remote_address = Listener.accept ()        print ("%s:%s Establish connection"% remote_address) while        True:            # Handle Client Port exception            Try:                data = CONN.RECV (1024x768). Decode ("Utf-8")            except Connectionreseterror as E:                print ("%s:%s disconnect"% remote_address)                conn.close ()                break            conn.send (bytes (Data.upper (), encoding= ' Utf-8 ')) if __name__ = = ' __main__ ':    Beimenchuixue_alone_socket ()

# This server implements a service that converts lowercase to uppercase, other client connections that need to be queued for processing client disconnect requests, that is, other clients are plugged in

North Gate blowing Snow: http://www.cnblogs.com/2bjiujiu/

Multi-threaded or co-process connection request schema diagram

    

 Thread-Processing Connection schema diagram

  

Multi-threaded Client listener instance implementation process

import    Socketfrom Threading Import Threaddef Beimenchuixue_handler (conn, remote_address): "" "Thread processing per Connection" "" # Handling Client Break exception            While True:try:data = Conn.recv (1024x768). Decode ("Utf-8") except Connectionreseterror as E: Print ("%s:%s disconnect"% remote_address) Break Conn.send (bytes (Data.upper (), encoding= ' Utf-8 ')) Conn. Close () def beimenchuixue_socket (): Listener = Socket.socket (socket.af_inet, socket. Sock_stream) Listener.bind ((' 0.0.0.0 ', 8000)) Listener.listen () while true:conn, remote_address = Listene R.accept () print ("%s:%s Establish connection"% remote_address) handler = Thread (Target=beimenchuixue_handler, args= (conn, R emote_address)) Handler.start () if __name__ = = ' __main__ ': Beimenchuixue_socket ()  

  

Object-oriented way to write multi-threaded sockets

Import socketfrom Threading Import ThreadClass Beimenchuixuehandle (Thread): Def __init__ (self, conn, remote_address): Super (). __init__ () Self.conn = conn Self.remote_address = remote_address def run (self): Whil E True:try:data = Self.conn.recv (1024x768). Decode ("Utf-8") except Connectionreseterror            As E:print ("North Gate Blown Snow") print ("%s:%s disconnect"% self.remote_address) break Self.conn.send (bytes (Data.upper (), encoding= ' Utf-8 ')) Self.conn.close () class Beimenchuixuehandlesocket:def __ Init__ (self): Self.listener = Socket.socket (socket.af_inet, socket.        SOCK_STREAM) def _handle (self, conn, remote_address): handler = Beimenchuixuehandle (conn, remote_address)        Handler.start () def socket_run (self, IP, port): Self.listener.bind ((IP, Port)) Self.listener.listen () While true:conn, remote_address = Self.listener.accept (Print ("North Gate Blown Snow") print ("%s:%s Establish connection"% remote_address) self._handle (conn, remote_address) if __name__ = = ' __main__ ': Listener = Beimenchuixuehandlesocket () listener.socket_run (ip= ' 0.0.0.0 ', port=8000)

  

co-way write socket (Requires the installation of the Gevent module, which natively supports semantics async await in the Python3.5 version)

Import socketimport geventfrom gevent import monkeyclass beimenchuixuesocket:def __init__ (self, IP, port): # Auto Mark clogging, to be placed before all clogging instances monkey.patch_all () Self.listener = Socket.socket (socket.af_inet, socket.            SOCK_STREAM) Self.ip = IP Self.port = Port def _handler (self, conn, remote_address): while True:                Try:data = Conn.recv (1024x768). Decode ("Utf-8") except Connectionreseterror as E: Print ("North Gate Blown Snow") print ("%s:%s disconnect"% remote_address) Break Conn.send (bytes (d Ata.upper (), encoding= ' Utf-8 ')) conn.close () def forever_run (self): Self.listener.bind ((Self.ip, Self.por T)) Self.listener.listen () while true:conn, remote_address = self.listener.accept () p Rint ("North Gate Blowing Snow") print ("%s:%s Establish connection"% remote_address) # initiates the process connection Gevent.spawn (Self._handler, Conn, remote_address) if __name__ = = ' __main__ ': Beimenchuixue = Beimenchuixuesocket (' 0.0.0.0 ', 8000) Beimenchuixue.forever_run () 

  

Experience:

1. Socket is the most basic communication mode of the network, and also the Communication Foundation of the upper application.

2. The server in order to respond to multiple requests at the same time, you can select processes, threads, and other methods to process the incoming requests, the general choice thread or the association

3. CPython because the Gil guarantees that a nucleus can only run one thread at a time, a multi-process in a sense just opens up multiple threads, and the process overhead is not a good method, that is, asynchronous programming for Python or using a thread or a co-process

4. The python thread, the coprocessor is only suitable for IO-intensive, not CPU-intensive, because the Gil's existence is essentially a single thread

python--oriented network programming-socket principle

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.