Python network programming Learning

Source: Internet
Author: User
Tags socket connect

Review:

Simple and clear! Review Network Programming and learn python by the way.

I. Socket

A socket is a network application on a specific network protocol (such as TCP/IP, ICMP/IP, UDP/IP) suite.ProgramThe provider provides the currently portable standard objects. 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.

Ii. 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. Generally, it takes six steps to establish a server connection.

Step 2 is to 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 ).

Step 2 is to bind the socket (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.

Step 2. 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.

Step 2: 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.

Step 2 is 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. The excess data is buffered at the receiver. When you call Recv later, excess data will be deleted from the buffer.

Step 2: After the transmission ends, the server calls the close method of socket 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.

 

3. A simple example of server-Client Communication

Server:

 

Import Socket
S = Socket. socket ()
S. BIND (( ' XXX. XXX ' , Xxxx )) # IP address and port number
S. Listen ( 5 )
CS, address = S. Accept ()
Print   ' Got connected from ' , Address
CS. Send ( ' Byebye ' )
RA = CS. Recv ( 512 )
Print RA
CS. Close ()

 

 

Client:

 

Import Socket
S = Socket. socket ()
S. Connect (( ' XXX. XXX ' , Xxxx )) # The IP address and port number of the server program are the same.
Data = S. Recv ( 512 )
S. Send ( ' Hihi ' )
S. Close ()
Print   ' The data already ed is ' , Data

 

 

Run:

Test on the local machine (in Windows, you can change the IP address to the IP address of the local machine, the port number is above 1024, and Windows will retain the IP address below 1024), run -- cmd -- to enter the command line mode

First, the python server program, and then the python client program.

You can also use the Telnet IP address and port number after starting the server program.

Allow the server to continuously accept connections

Server. py

 

Import Socket
S = Socket. socket ()
S. BIND (( ' 192.168.43.small ' , 2000 ))
S. Listen ( 5 )

While   1 :
CS, address = S. Accept ()
Print   ' Got connected from ' , Address
CS. Send ( ' Hello I am server, welcome ' )
RA = CS. Recv ( 512 )
Print RA
CS. Close ()

 

 

Test whether two sockets exist in one program.

Client. py

Import Socket
S = Socket. socket ()
S. Connect (( ' 192.168.43.small ' , 2000 ))
Data = S. Recv ( 512 )
Print   ' The data already ed is/n ' , Data
S. Send ( ' Hihi I am Client ' )

Sock2 = Socket. socket ()
Sock2.connect (( ' 192.168.43.small ' , 2000 ))
Data2 = Sock2.recv ( 512 )
Print   ' The data already ed from server is/n ' , Data2
Sock2.send ( ' Client send use sock2 ' )
Sock2.close ()

S. Close ()

Reprinted statement:This article from http://www.cppblog.com/lai3d/archive/2008/02/19/42919.html

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.