Discover python network programming pdf, include the articles, news, trends, analysis and practical advice about python network programming pdf on alibabacloud.com
Python network programming: basic use of socketserver, pythonsocketserverContent:
Introduction to socketserver
Use of socketserver
Asynchronous Server of socketserver
Start Time:
You can also use socketserver to create a socketSocketserver introduction:
Socketserver is an advanced module in the standard library.
Socketserver simplifies the code for crea
theSetup ()andFinish ()methods, and provideSelf.rfileandSelf.wfileAttributes.Self.rfileandSelf.wfileCan is read or written, respectively, to get the request data or return data to the Client.
Note that point 2,with respect to the Setdaemon property of the thread object, True indicates that the server thread is a background thread, and when no activity is connected to the background thread, the program executes automatically exits, and when there is no active Non-background thread activi
Baseserver.finish_request method and executes self. Requesthandlerclass () is the construction method that executes the custom Myrequesthandler (automatically calls the constructor of the base class Baserequesthandler, which in turn calls the Handle method of Myrequesthandler)
Threadingtcpserver related source code:Baseserver TCPServer threadingmixin ThreadingtcpserverRequestHandler related source codeSocketserver.baserequesthandlerInstance:Server-Side ClientSOURCE Streamlining:View CodeAs
request handler class.Next you have to instantiate a server class that, during instantiation, needs to pass in the service-side address and the class of the request handle.3.Then Call the Handle_request () or Serve_forever () method of the server object to process one or many requests.Then, execute the handle_request () or Serve_forever () method of the server object to process one or more requests.4.Finally, call Server_close () to close the socket.Finally, execute the Server_close () method t
We usually use on the computer program such as: Thunder, WORD,QQ, Firefox, etc., these unified called application software, Python development is also the main development of this application software.If QQ this application wants to run, we must first download QQ installs the software, then installs on the operating system,But the operating system is also software, but it is a system software, software to allow must be on the hardware, that is, must h
, PORT))8 9 whileTrue:Tenmsg = input (">>>:"). Strip () One ifLen (msg) = =0: A Continue - Client.sendall (Msg.encode ()) - thedata = CLIENT.RECV (1024) - - Print('Received', Data.decode ())First open the server, and then turn on client 1, client 2, in the client 1 input information can receive server response information, in the client 2 input information can not receive server response information, at this time disconnect client 1, client 2 can receive the service side of th
description
s.recv ()
receive TCP data
s.send ()
send TCP data
s.sendall ()
full send TCP Data
s.recvfrom ()
receive UDP data
s.sendto ()
send UDP data
socket function for public use
description
s.getpeername ()
The address of the remote end of the current socket is connected to
s.getsockname ()
First, Introduction:Python UDP is no connection, no TCP three handshake, error retransmission mechanism, hair, just send, collect, efficiency than TCP high, applied to the data frame is not high, such as video, audio transmissionSecond, the realization process:The server-side process is as follows:
1. Creating a UDP socket
2. Bind an interface to allow the client to connect
3. Acceptance of data
The client process is as follows:
1. Create a socket
2. Send
to other new clientsNewsocket, clientaddr =tcpsersocket.accept () whileTrue:#receive data sent from each other, maximum 1024 bytes receivedRecvData = NEWSOCKET.RECV (1024) #if the length of the received data is 0, it means that the client has closed the link ifLen (RecvData) >0:Print 'recv:', RecvDataElse: Break #send some data to the clientSendData = Raw_input ("Send:") newsocket.send (senddata)#closing the socket for this client service, as long as it is turne
clientNewsocket.send ("Thank you!")#closing the socket for this client service, as long as it is turned off, means that it can no longer serve the client, and if service is required, it can only reconnect againnewsocket.close ()#turning off the listener socket, as long as the socket is closed, means that the entire program can no longer receive any new client connectionsTcpsersocket.close ()TCP Client TCP client build processTCP client is much simpler than the server side, if the server is the
) 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,
Today learned a Python network programming, feel relatively simple, that is, the basic steps, Create--bind---listen-->receive/send---->close.Also learn a TCP version of the server programming framework that can support multiple socket connections. The feeling is also possible. The simplest of these frameworks is Tcpser
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.