Python 3.x Study Notes 13 (Network Programming socket), pythonsocket

Source: Internet
Author: User

Python 3.x Study Notes 13 (Network Programming socket), pythonsocket

1. Protocol
Http, smtp, dns, ftp, ssh, snmp, icmp, dhcp...

2. Layer 7 OSI
Application, representation, session, transmission, network, data link, physical

3. socket:

Encapsulation of all upper-layer protocols

4. Common socket Functions
1) sk. bind (address)

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.


2) sk. listen (backlog)

Start listening for incoming connections. Backlog specifies the maximum number of connections that can be suspended before a connection is rejected.

Backlog = 5 indicates that the kernel has received a connection request, but the maximum number of connections that the server has not called accept for processing is 5. This value cannot be infinitely large because the connection queue needs to be maintained in the kernel.

3) sk. setblocking (bool)

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

4) sk. accept ()

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

5) sk. connect (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.

6) sk. connect_ex (address)

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

7) 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.

8) sk. recvfrom (bufsize [. flag])

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.

9) sk. send (string [, flag])

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.

10) sk. settimeout (timeout)

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)

11) sk. sendto (string [, flag], address)

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.

12) sk. getpeername ()

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

13) sk. getsockname ()

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

14) sk. fileno ()

Socket file descriptor

Note:The above functions are used for reference in the blog https://www.cnblogs.com/aylin/p/5572104.htmlthis blog is very detailed recommendations for reference.

Exercise

Client

Import socketclient = socket. socket () # declare the socket type and generate the socket connection object client. connect ('localhost', 6565) client. send (B 'holle world! ') # Only byte data = client. recv (1024) can be used to transmit data #1024 print ('recv:', data) client. close ()

Server

Import socketserver = socket. socket () server. bind ("localhost", 6565 ))
Server. listen () # listen
Conn, addr = server. accept () # Wait
Data = conn. recv (1024) print ('recv: ', data) conn. send (data. upper () # return data
Server. close ()

 

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.