Python Core Programming (second Edition)--Network programming

Source: Internet
Author: User

1. Introduction

A server is a software or hardware that provides the "service" the customer needs. The sole purpose of the server's existence is to wait for the customer's request, to serve these customers, and then wait for other requests.

2. Socket: Communication Endpoint

A socket is a computer network data structure that has the concept of "communication endpoint". Networked applications are required to create sockets before any communication can begin. Just like the telephone socket, there is no way to communicate without it. There are two types of sockets, namely file-based and network-based.

Unix sockets are the first socket family we're going to introduce. Its "family name" is Af_unix (also called af_local in the POSIX1.G standard), which means "address family: UNIX". Because two processes are running on the same machine, and these sockets are file-based. Therefore, their underlying structure is supported by the file system.

Another socket is web-based and has its own family name: Af_inet, or "address family: Internet". There is also an address family af_inet6 is used for Internet Protocol version 6th (IPV6) addressing. Of all address families, Af_inet is the most widely used one. Python 2.5 adds support for a Linux socket: af_netlink (no connection [see below]) The socket family allows the IPC between user code and kernel code to use the standard BSD socket interface. Moreover, this approach is more graceful and more secure than cumbersome schemes such as adding new system calls to the operating system, proc file system support, or "IOCTL".

The valid port number range is 0 to 65535. Where the port number less than 1024 is reserved for the system. If you are using a Unix operating system, the reserved port number (and its corresponding service/protocol and socket type) can be obtained through the/etc/services file. A list of commonly used port numbers can be obtained from the following website: http://www.iana.org/assignments/port-numbers.

No matter which address family you use. There are only two types of sockets. One is a connection-oriented socket, which means that a connection must be established before communication, just like when you call a friend. This means of communication is also known as "virtual circuit" or "flow socket". Connection-oriented communication methods provide sequential, reliable, and not repetitive data transfer, and are not added to the boundaries of the database. This also means that each message to be sent may be split into multiple parts, each of which will not be more or less correctly arrived at the destination. They are then re-assembled in order to pass to the waiting application.

The primary protocol for implementing this connection is the Transmission Control Protocol (TCP). To create a TCP socket, you specify that the socket type is sock_stream when it is created. The TCP socket uses the name Sock_stream, which expresses its characteristics as a stream socket. Because these sockets use Internet Protocol (IP) to find hosts in the network, the entire system is typically referred to by both protocols (TCP and IP), TCP/Wireless.

The exact opposite of a virtual circuit is a datagram-type, non-connected socket. This means that you can communicate without establishing a connection. However, the order of data arrival, reliability and data repeatability can not be guaranteed. The datagram preserves the data boundaries, which means that the data is not split into small chunks like a connection-oriented protocol.

The primary protocol for implementing this connection is the User Datagram Protocol (that is, UDP). To create a UDP socket, you specify that the socket type is SOCK_DGRAM when it is created. Because these sockets use Internet protocols to find hosts in the network, the entire system is typically referred to by these two protocols (UDP and IP), that is, UDP/IP.

3. Network programming in Python

The socket () function in the module is used to create the socket. Sockets also have their own set of functions to provide socket-based network traffic. You use the Socket.socket () function to create a socket. Its syntax is as follows:

Sockets (Socket_family, Socket_type, protocol=0)

Socket_family can be Af_unix or af_inet. Socket_type can be sock_stream or sock_dgram. Protocol is generally not filled, the default value is 0.

Create a TCP/IP socket that you want to call Socket.socket ():

Tcpsock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)

To create a UDP/IP socket:

Udpsock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)

Because there are too many properties in the socket module. We made an exception here by using the ' from module import * ' statement. With the ' from socket import * ', we take all the attributes in the socket module into our namespace, which can greatly reduce our code.

When we create a socket object, all of the interactions are made through the method call to the socket object.

Common functions for Socket objects
Function Describe
Server-side socket functions
S.bind () Bind address (host, port number pair) to socket
S.listen () Start TCP snooping
S.accept () Passive acceptance of TCP client connections, (blocking) waiting for a connection to arrive
Client socket functions
S.connect () Active initialization of TCP server connections
S.CONNECT_EX () Extended version of the Connect () function, which returns an error code instead of throwing an exception when an error occurs
Socket functions for public use
S.RECV () Receiving TCP Data
S.send () Sending TCP data
S.sendall () Complete sending of TCP data
S.recvfrom () Receive UDP data
S.sendto () Send UDP data
S.getpeername () The address of the remote that is connected to the current socket
S.getsockname () Address of the current socket
S.getsockopt () Returns the parameters of the specified socket
S.setsockopt () To set parameters for a specified socket
S.close () Close socket
blocking-oriented Socket Methods
S.setblocking () To set the blocking and non-blocking mode for sockets
S.settimeout () To set the timeout period for blocking socket operations
S.gettimeout () Get the timeout for blocking socket operations
Functions for file-oriented sockets
S.fileno () File descriptor for sockets
S.makefile () Create a file that relates to the socket

Core Tip : When running a network application, it's a good idea to execute the server and client programs on different computers.

Create a TCP server:

1SS = Socket ()#Create a server socket2Ss.bind ()#bind an address to a socket3Ss.listen ()#Listening to Connections4Inf_loop:#Server Infinite loop5cs = ss.accept ()#Accept connections from customers6Comm_loop:#Communication Cycle7CS.RECV ()/cs.send ()#dialog (receive and send)8Cs.close ()#Close Client sockets9Ss.close ()#to turn off server sockets (optional)
View Code

All sockets are created with the Socket.socket () function. The server needs to "sit on a port" to wait for the request. So they must be "bound" to a local address. Because TCP is a connection-oriented communication system, you need to complete some settings before the TCP server can start working. The TCP server must have a "listen" (incoming) connection, and after the setup is complete, the server can enter an infinite loop. A simple (single-threaded) server calls the Accept () function to wait for the connection to arrive. By default, the Accept () function is blocked, that is, the program is in a pending state until the connection arrives. Sockets also support non-blocking mode. Once a connection is received, the Accept () function returns a separate client socket for subsequent communication. Using a new customer socket is like transferring a customer's phone call to a customer service person. When a customer calls in, the switchboard calls and then transfers the call to the right person to handle the customer's needs. This allows the switchboard to be vacated, that is, the initial server socket, so that the operator can wait for the next call (the customer's request), while the previous customer and the corresponding customer service staff are on the other line in their own dialogue. Similarly, when a request arrives, create a new port and then talk to the client directly on that port, so that the primary port can be vacated to accept connections from other clients. After the temporary sockets are created, the communication can begin. Both the client and the server use the newly created socket to send and receive data until one of the communications has closed the connection or sent an empty string, and the communication is over.

Core Tip : Create a thread to handle a customer request.

Creating a new thread or process to complete communication with the customer is a very common means. The Socketserver module is a high-level socket communication module based on the socket module, which supports the processing of client requests in new threads or processes.

Creating a TCP client: It is easier to create a TCP client relative to the server.

1 # Create a customer socket 2 # try to connect to the server 3 # Communication Cycle 4 # dialog (send/Receive) 5 # Close Client sockets
View Code

All sockets are created by the Socket.socket () function. Once the client has a socket, the Connect () function can be called immediately to connect to the server. Once the connection is established, you can start a conversation with the server. After the conversation is over, the customer can close the socket and end the connection.

run client and server programs: The server is a passive side that creates itself and then passively waits for a connection. And the customer is the active side, it is the initiative to establish a connection. So: to open the server first, after opening the customer.

Core tip : Graceful exit and call to the server's close () function

One way to exit "amicably" is to place an infinite loop of the server in a try clause of a try-except statement and catch Eoferror and Keyboardinterrupt exceptions. In the exception handling clause, call the close () function to close the server's socket.

Create a UDP server: because the UDP server is not connection-oriented, you do not have to do as many setup work as a TCP server. In fact, there's no need to set anything, just wait for the connection to come in.

1 # Create a server socket 2 # Binding Server Sockets 3 # Server Infinite loop 4 # dialog (receive and send) 5 # Turn off server sockets
View Code

Another important difference between UDP and TCP servers is that, because the datagram socket is not connected, the client's connection cannot be forwarded to another socket for subsequent communication. These servers just accept the message and, if necessary, return a result to the customer.

Create a UDP client:

1 # Create a customer socket 2 # Communication Cycle 3 # dialog (send/Receive) 4 # Close Client sockets
View Code

The UDP client's loop is essentially the same as the TCP client. The only difference is that we don't have to make a connection to the UDP server first, but send the message out and wait for the server to reply.

Socket Module Properties

Socket Module Properties
Property name Describe
Data properties
Af_unix, Af_inet, Af_inet6 Python-Supported socket families
So_stream, So_dgram Socket type (TCP = stream, UDP = datagram)
Has_ipv6 Flag variable indicating whether IPV6 is supported
Abnormal
Error Socket-related errors
Herror Host and address-related errors
Gaierror Address-related errors
Timeout Timeout
Function
Socket () Creates a socket object with the specified address family, socket type, and protocol type (optional)
Socketpair () Creates a pair of socket objects with the specified address family, socket type, and protocol type (optional)
FROMFD () Creates a socket object with an already opened file descriptor
Data properties
SSL () Initializes a Secure Sockets Layer (SSL) on the socket. No certificate validation is done.
Getaddrinfo () Get address information
Getfqdn () Returns the name of the full domain
GetHostName () Get the current host name
GetHostByName () The corresponding IP address is obtained by host name
GETHOSTBYNAME_EX () Extended version of GetHostByName (), returns the host name, all aliases and IP address lists of the host.
GETHOSTBYADDR () Gets the DNS information from the IP address, returning a 3-tuple similar to GETHOSTBYNAME_EX ().
Getprotobyname () The corresponding number is obtained by the protocol name (such as ' TCP ').
Getservbyname () Get the corresponding port number or vice versa by the service name
Getservbyport () Of the two functions, the protocol names are optional.
Ntohl ()/ntohs () Convert an integer from network byte order to host byte order
Htonl ()/htons () Convert an integer from host byte order to network byte order
Inet_aton ()/inet_ntoa () Convert IP addresses to 32-bit integers and inverse functions. (Valid only for IPV4 addresses)
Inet_pton ()/inet_ntop () Turn IP addresses into binary format and inverse function. (Valid only for IPV4 addresses)
Getdefaulttimeout ()/setdefaulttimeout () Get/Set Default socket time-out, in seconds (floating point)

4. *socketserver Module

Socketserver is a high-level module in the standard library. Used to simplify the implementation of network clients and servers.

Class of the Socketserver module
Class Describe
Baseserver Contains the core functions of the server and the hook function of the hybrid (Mix-in) class. This class is used for derivation and is not generated directly. For class objects of this class, you can consider using TCPServer and udpserver.
Tcpserver/udpserver Basic Network Synchronization TCP/UDP Server

unixstreamserver/

Unixdatagramserver

Basic file-based synchronization TCP/UDP server

forkingmixin/

ThreadingMixIn

Implements the core process or threading functionality for mixing with server classes (mix-in) to provide some asynchronous features. Do not directly generate objects of this class

forkingtcpserver/

Forkingudpserver

Combination of Forkingmixin and tcpserver/udpserver

threadingtcpserver/

Threadingudpserver

Combination of ThreadingMixIn and tcpserver/udpserver
Baserequesthandler Contains core functionality for processing service requests. To derive only new classes, do not directly generate objects of this class, consider using Streamrequesthandler or Datagramrequesthandler

streamrequesthandler/

Datagramrequesthandler

An implementation of the request processing class for the TCP/UDP server

Create a SOCKETSERVERTCP server: in your code, first import our server class, and then define the host constants as before. After the host constant is our request processor class, then the boot code.

1  #!/usr/bin/env python2 3   fromSocketserverImport(TCPServer as TCP, Streamrequesthandler as SRH)4   fromTimeImportCTime5 6HOST ="'7PORT = 215678ADDR =(HOST, PORT)9 Ten classMyrequesthandler (SRH): One     defhandle (self): A         Print '... connected from:', Self.client_address -Self.wfile.write ('[%s]%s'%(CTime (), Self.rfile.readline ())) -  theTcpserv =TCP (ADDR, Myrequesthandler) - Print 'waiting for connection ...' -Tcpserv.serve_forever ()
View Code

Create a SOCKETSERVERTCP client:

1 #!/usr/bin/env python2 3  fromSocketImport*4 5HOST ='localhost'6PORT = 215677Bufsiz = 10248ADDR =(HOST, PORT)9 Ten  whileTrue: OneTcpclisock =socket (af_inet, sock_stream) A Tcpclisock.connect (ADDR) -data = Raw_input ('>') -     if  notData: the          Break -Tcpclisock.send ('%s\r\n'%data) -data =tcpclisock.recv (Bufsiz) -     if  notData: +          Break -     PrintData.strip () +Tcpclisock.close ()
View Code

5. Twisted Framework Introduction

Python Core Programming (second Edition)--Network programming

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.