Python socket, python socket

Source: Internet
Author: User

Python socket, python socket
1. Client/Server Architecture

What is the client/server architecture? For different people, it means different things. It depends on who you ask and describe software or hardware systems. In either of these two cases, the premise is simple: the server is a series of hardware or software that provides the required "service" for one or more clients (service users ". It has the sole purpose of waiting for client requests, responding to them (providing services), and then waiting for more requests. On the other hand, the client contacts the server for a specific request, sends necessary data, waits for the server's response, and finally completes the request or gives the cause of the fault. The server runs infinitely and continuously processes requests. The client sends a one-time request to the service, receives the service, and ends transactions between them. The client may send other requests again after a period of time, but these requests are treated as different transactions.

2. Client/Server programming 2.1 socket

Socket originated in 1970s and is part of Berkeley UNIX (bsd unix) at the University of California. Therefore, sometimes you may have heard of a socket called a Berkeley socket or a BSD socket. A socket is initially created for applications on the same host so that one program (another process) running on the host can communicate with another running program. This is the so-called Inter-Process Communication (IPC ).There are two types of sockets: file-based and network-oriented. UNIX socket is the first family of sockets we talk about. It has a "family name" AF_UNIX (also known as AF_LOCAL, specified in POSIX1.g), which represents the address family ): UNIX. Most popular platforms, including Python, use the terminologies family and Their Abbreviations AF. Other older systems may express the address family as domain) or protocol family, which is abbreviated as PF rather than AF. Similarly, AF_LOCAL (in 2000 ~ Will replace AF_UNIX. However, considering backward compatibility, many systems use both, but use different aliases for the same constant. Python is still using AF_UNIX. Because two processes run on the same computer, these sockets are all file-based, which means
The system supports their underlying infrastructure. This makes sense, becauseA file system is a shared constant between multiple processes running on the same host..

The second type of socket is based on the network. It also has its own family name AF_INET or the address family: Internet. Another address family, AF_INET6, is used for Internet Protocol 6th (IPv6) addressing. In addition, there are other address families, either Professional, outdated, rarely used, or unimplemented. Among all address families, AF_INET is currently the most widely used.

Python 2.5 introduces support for special types of Linux sockets. SocketAF_NETLINK familyEnable IPC between user-level and kernel-level code using the standard BSD socket interface. Another feature for Linux (added in Python 2.6) supports transparentInter-process communication (TIPC) Protocol. TIPC allows machines in computer clusters to communicate with each other without IP address-based addressing. Python's support for TIPC is presented in the AF_TIPC family.

2.2 socket address: Host-Port Pair

Valid port numbers range from 0 ~ 65535 (although the port number smaller than 1024 is reserved for the system ).

2.3 connection-oriented socket

TCP socket. SOCK_STREAM must be used as the socket type.

2.4 connectionless socket

The main protocol for implementing this connection type is the User Datagram Protocol (UDP ). To create a UDP socket, you must use SOCK_DGRAM as the socket type.

3. Network Programming in python 3.1 socket module attributes

Socket creation:
socket(socket_family, socket_type, protocol=0)tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)ud pSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

Socket object method:

Server creation:
Ss = socket () # create a server socket ss. bind () # bind the socket to the address to ss. listen () # Listening to inf_loop: # server infinite loop cs = ss. accept () # accept client connection comm_loop: # communication loop cs. recv ()/cs. send () # receive/send a dialog) cs. close () # close the client socket ss. close () # close server socket # (optional)

We recommend that you call the close () method when implementing an intelligent exit solution for the server.

Client creation:
Cs = socket () # create a client socket cs. connect () # Try to connect to the server comm_loop: # communication loop cs. send ()/cs. recv () # Send/receive cs. close () # close the client socket
I/O multiplexing refers to the use of a thread to check the readiness of multiple file descriptors (sockets), such as calling the select and poll functions and passing in multiple file descriptors, if a file descriptor is ready, it is returned. Otherwise, it is blocked until it times out. After the status is ready, you can perform real operations in the same thread or start thread execution (for example, using the thread pool ). In this way, when processing 1000 connections, only one thread needs to monitor the ready state, and one thread can be opened for each ready connection. In this way, the number of required threads is greatly reduced, reduces the memory overhead and the CPU overhead of context switching.

For example, a tcp server is simulated to process 30 customer sockets.

Assume that you are a teacher who asks 30 students to answer a question and check whether the student is correct. You have the following options:

1. First option: Check one by one in sequenceFirst, check A, then B, then C, D... If there is a student card master in the middle, the class will be delayed.
This mode is like processing a socket cyclically without the concurrency capability.
2. Option 2: You Create 30 sub-bodiesCheck whether the answer of a student is correct. This is similar to creating a process or thread to process connections for each user.
3. The third option is you Standing on the podium and waiting, who answered the question and raised his hand. At this time, C and D raise their hands, indicating that they have finished answering the questions. You should check the answers of C and D in sequence, and then go back to the podium and wait. At this time, E and A raise their hands and then process E and...
This is the IO Reuse Model, and select, poll, and epoll in Linux do this. Register the fd corresponding to the user socket into epoll, and then epoll will help you monitor which sockets have messages arriving, so as to avoid a lot of useless operations. At this time, the socket should adopt Non-Blocking Mode.
In this way, the entire process will be blocked only when the select, poll, and epoll calls are called. Sending and receiving client messages will not be blocked, and the whole process or thread will be fully utilized. Event-drivenThe reactor mode. Method: windows python:     Available: select Mac Python:     Available: select Linux Python:     Available: select, poll, epoll  Select method: Select-manage multiple socket connections in a single-threaded network service middleware program. The prototype of select is (rlist, wlist, xlist [, timeout]). rlist is the object waiting for reading, wlist is the object waiting for writing, and xlist is the object waiting for exception, the last one is an optional object and specifies the waiting time, in seconds. The Return Value of the select () method is the three tuples of the prepared object. If no object is ready within the timeout time, the return value is an empty list, it uses Round Robin to Implement Asynchronous Communication. For more information: http://www.cnblogs.com/wupeiqi/articles/5040823.html Server:
#! /Usr/bin/python 'test TCP Server' from socket import * from time import ctime import select import sys HOST = ''PORT = 21567 BUFSIZ = 1024 ADDR = (HOST, PORT) tcpSerSock = socket (AF_INET, SOCK_STREAM) tcpSerSock. bind (ADDR) tcpSerSock. listen (5) input = [tcpSerSock, sys. stdin] # input is a list. The initial welcome socket and standard input while True: print 'Waiting for connection... 'tcpclisock, addr = tcpSerSock. accept () print '... connected from: ', addr input. append (tcpCliSock) # Add the service socket to the input list while True: readyInput, readyOutput, readyException = select. select (input, [], []) # select from input, and process the client's request connection (tcpSerSock) in turn, the message sent by the client (tcpCliSock ), and stdin for indata in readyInput: if indata = tcpCliSock: # process the message data sent by the client = tcpCliSock. recv (BUFSIZ) print data if data = '88 ': input. remove (tcpCliSock) break else: # process the sent message data = raw_input ('>') if data = '88 ': tcpCliSock. send ('% s' % (data) input. remove (tcpCliSock) break tcpCliSock. send ('[% s] % s' % (ctime (), data) if data = '88': break tcpCliSock. close () tcpSerSock. close ()
Client:
    #!/usr/bin/python            'test tcp client'            from socket import *      from time import ctime      import select      import sys            HOST = 'localhost'      PORT = 21567      BUFSIZ = 1024      ADDR = (HOST, PORT)      tcpCliSock = socket(AF_INET, SOCK_STREAM)      tcpCliSock.connect(ADDR)      input = [tcpCliSock,sys.stdin]            while True:          readyInput,readyOutput,readyException = select.select(input,[],[])          for indata in readyInput:              if indata==tcpCliSock:                  data = tcpCliSock.recv(BUFSIZ)                  print data                  if data=='88':                      break                 else:                  data = raw_input('>')                  if data=='88':                        tcpCliSock.send('%s' %(data))                      break                  tcpCliSock.send('[%s] %s' %(ctime(), data))          if data=='88':                break      tcpCliSock.close()  

 

3.2 socketserver Module

The SocketServer uses IO multiplexing and "multithreading" and "multi-process" internally to implement the Socket server that processes multiple client requests concurrently. That is, when each client request is connected to the server, the Socket server creates a "Thread" or "process" on the server to process all the requests of the current client.

Classes in this module

Basic usage: Create a SocketServer TCP Server
# The Request handler MyRequestHandler is a subclass of StreamRequestHandler in SocketServer # And Its handle () method is rewritten. This method has no behavior in the base class Request # by default. # Def handle (self): # pass # when receiving a message from the client, it calls the handle () method. The StreamRequestHandler # class regards the input and output sockets as objects of similar files, so we will use readline () to obtain client messages, # And Use write () to send the strings back to the client. From socketserver import TCPServer as TCP, StreamRequestHandler as SRHfrom time import ctimeHOST = ''PORT = '000000' ADDR = (HOST, PORT) class MyRequestHandle (SRH): def handle (self ): print ('... connected from: {0} ', self. client_address) self. wfile. write ('[% s] % s' % (ctime (), self. rfile. readline () tcpSever = TCP (ADDR, MyRequestHandle) print ('Waiting for connection') tcpSever. serve_forever ()
Create a TCP client
From socket import * HOST = 'localhost' PORT = '000000' BUFSIZ = 21567 ADDR = (HOST, PORT) while True: tcpClisocket = socket (ADDR, SOCK_STREAM) tcpClisocket. connect (ADDR) data = input ('>') if not data: break # because the handler class used here treats the set # The text communication is like a file, therefore, you must send the line terminator (carriage return and line feed) # While the server only keeps and reuses the Terminator sent here. When a message is returned from the slave server, use the strip () # function to process the message and use the linefeed automatically provided by the print statement. TcpClisocket. send ('% s \ r \ n' % data) data = tcpClisocket. recv (BUFSIZ) if not data: break print (data. strip () tcpClisocket. close ()
ThreadingTCPServer

The Soket server implemented by ThreadingTCPServer internally creates a"ThreadThis thread is used to interact with the client.

Reference: http://www.cnblogs.com/wupeiqi/articles/5040823.html

  • Create a class inherited from SocketServer. BaseRequestHandler
  • Class must define a method named handle
  • Start ThreadingTCPServer

 

# Start the server program # Run TCPServer. _ init _ method: Create a Socket object on the server and bind the IP address and port # Run BaseServer. the _ init _ method inherits the custom method from SocketServer. the class MyRequestHandle of BaseRequestHandler is assigned to self. requestHandlerClass # execute BaseServer. server_forever method, While loop always listens whether client requests arrive... # When the client connects to the server # execute ThreadingMixIn. process_request method. Create a "Thread" to process the request # execute ThreadingMixIn. process_request_thread method # execute BaseServer. finish_request method, execute self. requestHandl ErClass (): Execute the custom MyRequestHandler Constructor (automatically call the BaseRequestHandler constructor of the base class, and call the handle method of MyRequestHandler in this constructor) # server ##! /Usr/bin/env python #-*-coding: UTF-8-*-import SocketServerclass MyServer (SocketServer. baseRequestHandler): def handle (self): # print self. request, self. client_address, self. server conn = self. request conn. sendall ('Welcome to call 10086. Please input 1xxx, 0 to manual service. ') Flag = True while Flag: data = conn. recv (1024) if data = 'eg': Flag = False elif data = '0': conn. sendall ('going through may be recorded. balabala) else: conn. sendall ('enter again. ') if _ name _ =' _ main _ ': server = SocketServer. threadingTCPServer ('2017. 0.0.1 ', 8009), MyServer) server. serve_forever ()
ForkingTcpSever

The use and execution processes of ForkingTCPServer and ThreadingTCPServer are basically the same, except that "Thread" and "process" are created internally for the requester respectively ".

3.3 Twisted Module

Twisted is a completeEvent-driven network framework, Use itBoth use and complete Asynchronous Network applications and protocols can be developed. It provides a lot of support to build a complete system, including network protocols, threads, security and authentication, chat/IM, DBM and RDBMS database integration, Web/Internet, email, command line parameters, and GUI integration toolkit. Similar to SocketServer, most of Twisted functions exist in its class.

Twisted Reactor TCP Server

Create TSServProtocol based on the protocol class and override the connectionMade () and dataReceived () methods. When a client connects to the server, the connectionMade () method is executed, when the server receives some data sent by the client over the network, it calls the dataReceived () method.
The reactor is transmitted as a parameter of the method in data so that it can be accessed without being extracted by itself. In the last part of the server code, a protocol factory is created. It is called a factory because an instance of the "Manufacturing" protocol can be created every time an access connection is obtained. Then install a TCP listener in the reactor to check service requests. When it receives a request, it creates a TSServProtocol instance to process the transaction of that client.

from twisted.internet import protocol,reactorfrom time import ctimePORT=21567class TSServProtocal(protocol.Protocol):    def connectionMade(self):        clnt=self.clnt=self.transport.getPeer().host        print('...conected from:',clnt)    def dataReceived(self, data):        self.transport.write('[%s]%s'%(ctime(),data))factory=protocol.Factory()factory.protocol=TSServProtocalprint('waiting for conneciton')reactor.listenTCP(PORT,factory)reactor.run()
Twisted Reactor TCP client
from twisted.internet import protocol, reactorHOST='localhost'PORT=21567class TSClntProtocal(protocol.Protocol):    def sendData(self):        data=input('>')        if data:            print('...sending%...'%data)        else:            self.transport.loseConnection()    def connectionMade(self):        self.sendData()    def dataReceived(self, data):        print(data)class TSClntFactory(protocol.Factory):    protocol=TSClntProtocal    clientConnectionLost=clientConnectionFailed=\    lambda self,connector,reason:reactor.stop()reactor.connectTCP(HOST,PORT,TSClntFactory())reactor.run()

 

Related Article

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.