The "socket" of the Python standard library

Source: Internet
Author: User

Sockets are also commonly referred to as " sockets ". The two programs on the network realize the exchange of data through a two-way communication connection, one end of this connection is called a socket. The socket is the network connection endpoint. For example, when your Web browser requests a home page on www.fishc.com, your Web browser creates a socket and commands it to connect to the Www.fishc.com Web server host, and the Web server listens on a socket from the request. Both ends use their own sockets to send and receive information.
The socket module provides several functions for working with host names and addresses: gethostname () returns the hostname of the computer on which the program is running:

    1. #>>> Import Socket
    2. #>>> Socket.gethostname ()
    3. # ' Ricardo's computer '
    4. #>>>
Copy Code

gethostbyname (name) attempts to interpret the given host name as an IP address

    1. #>>> #socket. gethostbyname (' www.fishc.com ')
    2. # ' 101.37.44.185 '
    3. #>>> Socket.gethostbyname (' Ricardo's computer ')
    4. # ' 192.168.1.104 '
    5. #>>>
Copy Code

PS: First check whether the current computer can be interpreted. If not, an explanation request is sent to a remote DNS server (the remote DNS server may also forward the interpretation request to another DNS server until the request can be processed).         The gethostbyname function returns this IP address or throws an exception after a lookup failure.         Expand gethostbyname_ex (name) returns a tuple containing three elements: (a column of the primary host name for the given address, an optional hostname of the same IP address , A list of other IP addresses of the same interface on the same host )

    1. >>> #>>>socket.gethostbyname_ex (' www.fishc.com ')
    2. # (' bbgaws7ldwotai4akucb7ckfsnvzoz6m.aliyunwaf.com ', [' www.fishc.com '], [' 101.37.44.185 '])
Copy Code

The gethostbyaddr function works the same as GETHOSTBYNAME_EX, except that the argument you provide to it is an IP address string
The getservbyname (service,protocol) function requires a service name (such as ' Telnet ' or ' ftp ') and a protocol (such as ' TCP ' or ' UDP ') to return the port number used by the service:

    1. #>>>socket.getservbyname (' http ', ' TCP ')
    2. #80
    3. #>>>socket.getservbyname (' telnet ', ' TCP ')
    4. #23
Copy Code

Socket Communication Import socket
1. Socket ()

    1. Socket.socket (Family=af_inet, Type=sock_stream, proto=0, Fileno=none)
Copy Code

(1) Family = Af_inet represents communication between servers.

(2) fmily = Af_unix represents the communication between different UNIX processes.

(3) type = Sock_stream represents a TCP connection.

(4) type = Sock_dgram represents a UDP connection.

Ps:2, 3, 4 method for server2. Bind ()
Sk.bind (Address)
Binds the address to the socket. Address (host IP, port number), address must be a tuple.

3. Listen ()
Listen (backlog)
Listens for client connections. The backlog is an optional parameter that represents the maximum number of waiting connections.

4.accept ()
Accept the connection and return (conn,address). Where Conn represents the client's SK object,
Example:

    1. SK = Socket.socket ()
    2. Address = (' 127.0.0.1 ', 9080)
    3. Sk.bind (Address)
    4. Sk.listen (5)
    5. CONN,ADDR = Sk.accept ()
    6. Print (SK)
    7. Print (conn)
    8. Print (addr)
    9. Output Result:
    10. <socket.socket fd=3, Family=addressfamily.af_inet, Type=socketkind.sock_stream, proto=0, laddr= (' 127.0.0.1 ', 9080 ) >
    11. <socket.socket fd=4, Family=addressfamily.af_inet, Type=socketkind.sock_stream, proto=0, laddr= (' 127.0.0.1 ', 9080 ), raddr= (' 127.0.0.1 ', 35066) >
    12. (' 127.0.0.1 ', 35066)
Copy Code

5.recv ()
SK.RECV (bufsize)
Receive data. Where bufsize represents the maximum amount of data that can be received.

6.connect ()
Sk.connect (Address)
Connect the socket to the specified address. Address is represented by a tuple.

7.send ()
Sk.send (data)
Send data to the connected socket.

8.sendall ()
Like send, the internal calls the Send () method recursively, attempting to send all the data.
9.sendto ()
Sk.sendto (DATA,ADDR) to specify a remote address

notice! TCP (SOCK_STREAM), need to connect to the remote host to send data, send data with send (b ' character ') UDP (SOCK_DGRAM), do not need to connect, send data directly, send data with sendto (b ' character ', (Ip,port))
10.settimeout (Timeout)
Sets the timeout.

11.getpeername ()
Returns the address of the remote socket (address,port).

12.getsockname ()
Return your own socket address (address,port).

13.fileno ()
The file descriptor for the socket.

Example: Using the socket module for the Communication Service side:

    1. SK = Socket.socket ()
    2. Address = (' 127.0.0.1 ', 9080)
    3. Sk.bind (Address)
    4. Sk.listen (5)
    5. CONN,ADDR = Sk.accept ()
    6. data = CONN.RECV (1024)
    7. Print (str (data, ' UTF8 '))
Copy Code

Client:

    1. SK = Socket.socket ()
    2. Address = (' 127.0.0.1 ', 9080)
    3. Sk.connect (Address)
    4. INP = input (' >> ')
    5. Sk.send (Bytes (INP, ' UTF8 '))
Copy Code


The "socket" of the Python standard library

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.