Socket library for network programming-python

Source: Internet
Author: User

One, the network connection frequently uses the function

SK = Socket.socket (socket.af_inet,socket. sock_stream,0)
parameter one: Address cluster

socket.af_inet IPv4 (default)
Socket.af_inet6 IPv6

Socket.af_unix can only be used for single UNIX system interprocess communication

parameter two: type

socket. Sock_stream streaming socket, for TCP (default)
socket. SOCK_DGRAM datagram socket, for UDP

socket. Sock_raw the original socket, the ordinary socket can not handle ICMP, IGMP and other network messages, and Sock_raw May, second, Sock_raw can also handle special IPV4 messages, in addition, the use of the original socket, can be IP_ The HDRINCL socket option constructs an IP header by the user.
socket. SOCK_RDM is a reliable form of UDP that guarantees the delivery of datagrams but does not guarantee the order. Sock_ram is used to provide low-level access to the original protocol, which is used when certain special operations are required, such as sending ICMP packets. Sock_ram is typically used only by advanced users or by programs that are run by administrators.
socket. Sock_seqpacket Reliable Continuous Packet service

parameter three: protocol
(default) protocol related to a particular address family, if 0, the system will automatically select an appropriate protocol based on the address format and socket category .

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

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 the accept to process the maximum number of connections is 5
This value cannot be infinitely large because the connection queue is maintained in the kernel

sk.setblocking (BOOL)

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

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

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.

sk.connect_ex (address)

Ibid., except that there will be a return value, return 0 when the connection succeeds, and return the encoding when the connection fails, for example: 10061

sk.close ()

Close Socket

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.

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.

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.sendall (String[,flag])

sends data from a string to a connected socket, but attempts to send all data before returning. Successful return none, Failure throws an exception.

internally, the send is called recursively, sending all the content.

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

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

Second, get some address and host name

The socket module provides several functions for working with host names and addresses.

Socket.gethostname ()

GetHostName () returns the host name of the computer on which the program is running

Socket.gethostbyname (' www.jb51.net ')

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

GETHOSTBYNAME_EX (name)

It returns a tuple of three elements, namely a list of the primary host name for the given address, an optional hostname for the same IP address, and a list of other IP addresses for the same interface on the same host

Socket library for network programming-python

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.