Python 3.x Learning note 13 (Network programming socket)

Source: Internet
Author: User

1. Agreement
HTTP, SMTP, DNS, FTP, SSH, SNMP, ICMP, DHCP .... and other specific self-examination

2.OSI Seven Floor
Applications, representations, sessions, transports, networks, data links, physical

3.socket:

Encapsulation of all upper-level protocols

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

S.bind binds the socket to the address. The format of address addresses depends on the address family. Under Af_inet, address is represented as a tuple (host,port).


2) Sk.listen (backlog)

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

The backlog equals 5, indicating that the kernel has received a connection request, but the server has not yet called accept to handle the maximum number of connections 5 This value cannot be infinite because the connection queue is maintained in the kernel

3) sk.setblocking (BOOL)

Whether blocking (default true), if set to false, then the accept and recv when there is no data, the error.

4) sk.accept ()

Accepts the connection and returns (Conn,address), where Conn is a new socket object that can be used to receive and send data. Address is the location of the connection client. Incoming TCP Client connection (blocked) waiting for connection

5) Sk.connect (address)

The socket that is connected to the address. Generally, address is in the form of a tuple (Hostname,port) and returns a socket.error error if there is an error in the connection.

6) SK.CONNECT_EX (address)

Ditto, but there will be a return value, the connection succeeds when the return 0, the connection fails when the return encoding, for example: 10061

7) Sk.recv (Bufsize[,flag])

Accepts the data for the socket. The data is returned as a string, and bufsize specifies the maximum quantity that can be received. Flag provides additional information about the message, which can usually be ignored.

8) Sk.recvfrom (Bufsize[.flag])

Similar to recv (), but the return value is (data,address). Where data is the string that contains the received information, address is the socket addressing that sent the data.

9) Sk.send (String[,flag])

Sends data from a string to a connected socket. The return value is the number of bytes to send, which may be less than the byte size of the string. That is, the specified content may not be sent all.

Sk.settimeout (Timeout)

Sets the timeout period for the socket operation, and timeout is a floating-point number in seconds. A value of None indicates no over-time. In general, hyper-times should be set when a socket is just created, as they may be used for connected operations (such as client connections waiting up to 5s)

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

Sends the data to the socket, address is a tuple in the form of (Ipaddr,port), specifying the remote address. The return value is the number of bytes sent. This function is mainly used for UDP protocol.

Sk.getpeername ()

Returns the remote address of the connection socket. The return value is typically a tuple (ipaddr,port).

Sk.getsockname ()

Returns the socket's own address. Typically a tuple (ipaddr,port)

) Sk.fileno ()

File descriptor for sockets

Note: The above features are borrowed from blog https://www.cnblogs.com/aylin/p/5572104.html This blog description very detailed recommended to visit

Practice

Client

Importsocketclient= Socket.socket ()#declares the socket type and generates the socket connection object at the same timeClient.connect (('localhost', 6565)) Client.send (b'Holle world!')#transfer data can only be in byte typedata = CLIENT.RECV (1024)#One acceptable amount is 1024x768.Print('recv:', data) client.close ()

Service side

Import= socket.socket () server.bind('localhost', 6565)
Server.listen () # Monitor
CONN,ADDR = server.accept () # wait
= Conn.recv (1024x768)print('recv:', data) conn.send ( Data.upper ()) #返回数据
Server.close ()

Python 3.x Learning note 13 (Network programming socket)

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.