Chapter 16 Network Programming
? It should be a chapter on how to program communication between networks.
16.1.1 Client/server architecture
? Client requests access, server-side listens for requests, processes requests, and makes corresponding patterns
16.1.2 Client/server programming
? Server-side: Create a communication endpoint to use for listening. and determine its own location, both the URL
Client: Create a communication endpoint, establish a connection to the server
16.2 Socket: Communication Breakpoint
? is a communication necessary network data structure, like the telephone interface, it is a data interface
Socket address: Socket to have host address and port (0-65535)
Socket Type:
Connection-oriented: to establish a connection before communication: protocol implemented when TCP
No connection: Implementing the Protocol when UDP
Each of these sockets uses the IP protocol to find a host in the network.
16.3 Using Python for network programming communication
16.3.1 socket () module
? Socket.socket () function to create sockets
Set up TCP/IP,UDP/IP sockets separately
Tcpsock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
# The first is the family agreement, they are all network. The second one is type-oriented
Udpsock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM)
16.3.2 Socket method
? After you create a socket, you use the method it has to communicate
16.3.3 Creating a TCP server
Idea: Create sockets---Bound sockets to a local address---listen to a connection---an infinite loop to listen to a TCP client request (which will return a client socket)---Accept TCP data---Send TCP data---Close the socket---Close the server sockets
From socket Import *
From time import CTime
HOST = "# indicates that any valid address can be bound
PORT = 2157
Bufsiz = 1024 # keyword with a size of 1k
Adr= (HOST, PORT)
Tcpsersock = socket (Af_inet, SOCK_STREAM) # Create a network-based, TCP server socket
Tcpsersock.bind (ADR)
Tcpsersock.listen (5) # allows only 5 connections at a time
While True: # infinite loop answer
print ' Waiting for connecttion .... '
Tcpclisock, addr = Tcpsersock.accept () # Receive and save the client socket, client address
print ' ... conneted from: ', addr
While True:
data = Tcpclisock.recv (bufsiz) # Read buffer
If not data:
Break
Tcpclisock.send (' {0} {1} '. Format (CTime (), data)) #发送给客户端信息
Tcpclisock.close ()
Tcpsersock.close ()
16.3.4 Creating a TCP Client
Idea: Create a client socket---make a connection---send data (exit when send is empty)---receive data from the buffer (exit if receive fails)---Close the client socket
From socket Import *
HOST = ' localhost '
PORT = 21567
Bufsiz = 1024
ADDR = (HOST, PORT)
Tcpclisock = socket (af_inet, SOCK_STREAM)
Tcpclisock.connect (ADDR)
While True:
data = Raw_input (' > ')
If not data:
Break
Tcpclisock.send (data)
data = TCPCLISOCK.RECV (Bufsiz)
If not data:
Break
Print data
Tcpclisock.close ()
16.3.6 creating a UDP server
Idea: Create sockets---Bind sockets to a local address--Receive data---send data
From socket Import *
From time import CTime
HOST = "# indicates that any valid address can be bound
PORT = 2157
Bufsiz = 1024 # keyword with a size of 1k
Adr= (HOST, PORT)
Udpsersock = socket (af_inet, SOCK_DGRAM)
Udpsersock.bind (ADR)
While True:
print ' Waiting for message ... '
data, addr = Udpsersock.recvfrom (Bufsiz)
Udpsersock.sendto (' {0} {1} '. Format (CTime (), data), addr)
Print ' ... received from and returned to: ', addr
Udpsersock.close ()
16.3.7 Creating a UDP client
Idea: Create a client socket---Send data---receive data
From socket Import *
From time import CTime
HOST = ' localhost ' # indicates that any valid address can be bound
PORT = 2157
Bufsiz = 1024 # keyword with a size of 1k
Addr= (HOST, PORT)
Udpclisock = socket (af_inet, SOCK_DGRAM)
While True:
data = Raw_input (' > ')
If not data:
Break
Udpclisock.sendto (data, ADDR)
data, ADDR = Udpclisock.recvfrom (bufsiz)
If not data:
Break
Print data
Udpclisock.close ()
16.4 Socketserver Module
? is a standard library for simplifying the extensive sample code required to implement network clients and servers, encapsulating many of the required code.
164.1 creating a Socketserver-based TCP server
Idea: first inherit Streamrequesthandler create request processing class, override handle function---Call tcpserver use parameter "Request processing class" To generate TCP server---server infinite loop
From Socketserver import (TCPServer as TCP, Streamrequesthandler as SRH)
From time import CTime
HOST = ' '
PORT = 21567
ADDR = (HOST, PORT)
Class Myrequesthandler (SRH): # Defines the request class that inherits Streamrequesthandler, overrides the handle function (default is pass)
def handle (self):
print ' ... connected from: ', self.client_address
Self.wfile.write (' {0} {1} '. Format (CTime (), Self.rfile.readline ())) # Read and write separately
Tcpserv = TCP (ADDR, Myrequesthandler) # calls TCPServer requires parameter ADDR and request processing class
print ' Waiting for connection ... '
Tcpserv.serve_forever () # This server goes into an infinite loop
16.4.2 creating a Socketserver-based TCP client
Idea: Create a client socket---Send data---receive data---close a socket
From socket Import *
HOST = ' localhost '
PORT = 21567
Bufsiz = 1024
ADDR = (HOST, PORT)
While True:
Tcpclisock = socket (Af_inet, SOCK_STREAM) # with the previous difference here, the default behavior of the Socketserver request processor is to accept the connection, get the request, and close the connection, so each time a socket is created
Tcpclisock.connect (ADDR)
data = Raw_input (' > ')
If not data:
Break
Tcpclisock.send ('%s\r\n '% data)
data = TCPCLISOCK.RECV (Bufsiz)
If not data:
Break
Print Data.strip ()
Tcpclisock.close ()
16.5 Twisted Frame
? is a fully time-driven network framework that allows the use and development of fully asynchronous network applications and protocols
Network Programming Socket-socketserver