Python Study Notes 11 and python Study Notes

Source: Internet
Author: User
Tags socket connect

Python Study Notes 11 and python Study Notes
Network ProgrammingConcepts required for Network Programming 

The network architecture is a kind of adhesive that uses different devices and network systems connected with different media to achieve interoperability in different application environments and meet various business needs. The network architecture is a hierarchical method to solve the problem of mutual quality.

1.Layer-7 network (OSI) models:

Application Layer ---> provides network communication services for Applications

Presentation Layer ---> Data Representation

Session Layer ---> inter-host communication (between two application processes)

Transport Layer ---> end-to-end connection, isolating the upper and lower layers of the network protocol, so that the network application is irrelevant to the lower layer protocol

Network Layer ---> find the optimal path and forward data packets

Data link layer ---> error-free link connection

Physical Layer ---> binary transmission

2.Port

Is an abstract software structure, including some data structures and I/O buffers. Related to the Agreement.

3.The socket exists in the communication area.

A communication region is also called an address family. It is an abstract concept used to combine the common features of processes that communicate through sockets.

To ensure data correctness, the network byte sequence should be set in the network protocol, and the network byte sequence should be unified.

Three elements of network communication:

IP Address: used to indicate the host (IP address = network ID + host ID)

Port Number: the logical port used to identify the process

Transmission Protocol: TCP UDP

Network Communication is a process of constant encapsulation and resolution.

A Socket is a bridge between an application and a network driver. A Socket is created in an application and is associated with the driver through the binding operation.

Socket

A socket is a network application provider that provides portable standard objects for specific network protocols (such as TCP/IP, ICMP/IP, and UDP/IP. They allow programs to accept and connect, such as sending and receiving data. To establish a communication channel, each endpoint of network communication has a socket object.

Sockets are part of the core of bsd unix systems, and they are also adopted by many other UNIX-like operating systems, including Linux. Many non-bsd unix systems (such as ms-dos, windows, OS/2, mac OS, and most host environments) provide socket support in the form of libraries.

The three most popular socket types are stream, datax, and raw. Stream and datax sockets can directly interface with the TCP protocol, while raw socket interfaces to the IP protocol. However, sockets are not limited to TCP/IP.

Socket module ------- SOCKET () Module

The socket module is a very simple object-based interface that provides access to the low-layer BSD socket style network. This module can be used to implement client and server sockets. To create a simple server with TCP and stream sockets in python, you need to use the socket module. Use the functions and class definitions contained in this module to generate programs that communicate over the network.

SOCKET built-in method

Function Description
Server socket Functions  
S. bind ()

Bind the address (host, port number pair) to the socket

S. bind (address) binds the socket to the address.

The address format depends on the address family. In AF_INET, the address is expressed in the form of a tuple (host, port.

S. listen ()

Start TCP listening

Start listening for incoming connections. The add parameter specifies the maximum number of connections that can be suspended before a connection is denied.

If the parameter is 5, the kernel has received a connection request, but the server has not called accept to process the maximum number of connections.
This value cannot be infinitely large because the connection queue needs to be maintained in the kernel.

S. accept ()

Passively accept TCP customers' connections, and wait for the connection to arrive.

Accept the connection and return (conn, address). conn is a new socket object and can be used to receive and send data. Address is the address used to connect to the client.

Receive TCP client connection (blocking) waiting for connection arrival

Client socket Functions  
S. connect ()

Actively initialize the TCP server connection. The parameter is address)

Connect to the socket at address. Generally, the format of address is tuples (hostname, port). If a connection error occurs, the socket. error is returned.

S. connect_ex ()

Extended version of the connect () function. An error code is returned when an error occurs, instead of throwing an exception.

The same as above, but there will be a return value. When the connection is successful, 0 is returned, and the encoding is returned when the connection fails, for example: 10061

Common socket Functions  
S. recv ()

Receive TCP Data

  Sk. recv (bufsize [, flag])

Accept socket data. The data is returned as a string. The bufsize specifies the maximum number of messages that can be received. Flag provides other information about messages, which can be ignored.

S. send ()

Send TCP Data

Send the data in the string to the connected socket. The returned value is the number of bytes to be sent, which may be smaller than the size of the string. That is, all specified content may not be sent.

S. sendall ()

Send TCP Data

Send data in string to the connected socket, but try to send all data before returning. If None is returned successfully, an exception is thrown.

Internally, send all content by calling send recursively.

S. recvfrom ()

Receive UDP data

Similar to recv (), but the return value is (data, address ). Data is the string containing the received data, and address is the socket address for sending data.

S. sendto ()

Send UDP data

Sends data to the socket. The address is a tuples in the form of (ipaddr, port) and specifies the remote address. The returned value is the number of bytes sent. This function is mainly used for UDP.

S. getpeername ()

Remote address connected to the current socket

Returns the remote address of the socket. The returned value is usually a tuples (ipaddr, port ).

S. getsockname ()

Current socket address

Return the address of the socket. It is usually a tuple (ipaddr, port)

S. getsockopt () Returns the parameter of the specified socket.
S. setsockopt () Set the parameters of the specified socket
S. close () Disable socket
Module-oriented socket Functions  
S. setblocking ()

Set the blocking and non-blocking modes of sockets

Whether to block (True by default). If False is set, an error is returned if no data exists in accept and recv.

S. settimeout ()

Set the timeout time for blocking socket operations

Sets the superperiod of socket operations. timeout is a floating point number in seconds. If the value is None, there is no superperiod. Generally, it should be set when the socket is just created during off-peak periods, because they may be used for connection operations (for example, a client connection can wait up to 5 s)

S. gettimeout () Obtain the timeout value for blocking socket operations.
File-oriented socket Functions  
S. fileno () Socket file descriptor
S. makefile () Create a file associated with the socket
   

Six steps are required to establish a server connection:

  1.Create a socket object. Call the socket constructor.

Socket = socket. socket (familly, type)

The value of family can be AF_UNIX (Unix domain used for communication between processes on the same machine) or AF_INET (TCP and UDP for IPV4 protocol). As for the type parameter, SOCK_STREAM (stream socket), SOCK_DGRAM (Data PACKET socket), and SOCK_RAW (raw socket ).

  2.The socket is bound (assigned) to the specified address, socket. bind (address)

Address must be a dual-element tuples (host, port), host name or IP address + port number. If the port number is being used or retained, or the host name or IP address is incorrect, A socke. error exception is thrown.

  3.After binding, you must prepare a socket to accept the connection request.

Socket. listen (backlog)

Backlog specifies the maximum number of connections, at least 1. After receiving the connection request, these requests must be queued. If the queue is full, the request is rejected.

  4.The server socket waits for the customer to request a connection through the socket accept method:

Connection, address = socket. accept ()

When the accept method is called, the socket enters the 'waiting' (or blocking) state. When a customer requests a connection, the method establishes a connection and returns it to the server. The accept method returns a tuples containing two elements, such as (connection, address ). The first element (connection) is a new socket object through which the Server communicates with the customer. The second element (address) is the customer's internet address.

  5.In the processing phase, the server and the customer communicate (transmit data) through the send and recv methods ). The server calls send and uses a string to send messages to the customer. The send method returns the number of sent characters. The server uses the recv method to receive information from the customer. When calling recv, you must specify an integer to control the maximum amount of data received by this call. The recv method enters the 'bucket' state when receiving data, and returns a string to indicate the received data. If the number of sent messages exceeds the recv limit, the data is truncated. Extra data is buffered at the receiver. When you call recv later, excess data will be deleted from the buffer.

  6.When the transmission ends, the server calls the socket close method to close the connection.

It takes four steps to establish a simple customer connection.

Step 2: Create a socket to connect to the server socket = socket. socket (family, type)

Step 2: Use the socket connect method to connect to the server socket. connect (host, port ))

Step 2: the customer and the server communicate through the send and recv methods.

Step 2. After the end, the customer calls the close method of socket to close the connection.

In the following cases, TCP connections are used.

Import socketsk = socket. socket () address = ('2017. 0.0.1 ', 8000) sk. bind (address) sk. listen (2) print ("...... ") while True: conn, addr = sk. accept () print (addr) while True: try: date = conn. recv (1024) failed t Exception: break if not date: break print (str (date, "utf8") indium = input (">>>>:") conn. send (bytes (indium, "utf8") # conn, addr = sk. accept () # while True: # date = conn. recv (1024) # if not date: # conn, addr = sk. accept () # continue # print (str (date, "utf8") # indium = input (">>>:") # conn. send (bytes (indium, "utf8") # conn. close () conn. close ()Server import socketsk = socket. socket () address = ("Fig", 8000) sk. connect (address) while True: indium = input (">>>>:") if indium = "q": break sk. send (bytes (indium, "utf8") date = sk. recv (1024) print (str (date, "utf8") sk. close ()Client

Simple simulation of qq conversation (Socket module)

Import socket, subprocesssk = socket. socket () address = ('0. 0.0.0 ', 8000) sk. bind (address) sk. listen (2) print ("...... ") while True: conn, addr = sk. accept () print (addr) while True: try: date = conn. recv (1024) failed t Exception: break if not date: break print (str (date, "utf8") obj = subprocess. popen (str (date, "utf8"), shell = True, stdout = subprocess. PIPE) pai_result = obj. stdout. read () result_len = str (len (yield _result) print (result_len) conn. send (bytes (result_len, "utf8") conn. send (pai_result) conn. close ()Serverimport socketsk = socket. socket () address = ("Fig", 8000) sk. connect (address) while True: indium = input (">>>>:") if indium = "q": break sk. send (bytes (indium, "utf8") result_len = int (str (sk. recv (1024), "utf8") print (result_len) date = bytes () while len (date )! = Result_len: da = sk. recv (1024) date + = da print (str (date, "gbk") sk. close ()Client

Upload images through simple FTP

Import subprocessimport socketimport ossk = socket. socket () print (sk) address = ('2017. 0.0.1 ', 8000) sk. bind (address) sk. listen (3) print ('waiting ...... ') BASE_DIR = OS. path. dirname (OS. path. abspath (_ file _) while 1: conn, addr = sk. accept () while 1: data = conn. recv (1024) cmd, filename, filesize = str (data, 'utf8 '). split ('|') path = OS. path. join (BASE_DIR, 'tuian', filename) filesize = int (filesize) f = open (path, 'AB') has_rec Eive = 0 while has_receive! = Filesize: data = conn. recv (1024) f. write (data) has_receive + = len (data) f. close ()Serverimport socketimport ossk = socket. socket () address = ('2017. 0.0.1 ', 8000) sk. connect (address) BASE_DIR = OS. path. dirname (OS. path. abspath (_ file _) while True: indium = input ('>>> '). strip () # post | 11.png cmd, path = indium. split ('|') path = OS. path. join (BASE_DIR, path) filename = OS. path. basename (path) file_size = OS. stat (path ). st_size file_info = 'Post | % s | % s' % (filename, file_size) sk. sendall (bytes (file_info, 'utf8') f = open (p Ath, 'rb') has_sent = 0 while has_sent! = File_size: data = f. read (1024) sk. sendall (data) has_sent + = len (data) f. close () print ('uploaded successfully ')Client

 

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.