Python Socket class Library T Introduction

Source: Internet
Author: User


first, what is the socket

1. Sockets are also commonly referred to as "sockets", which describe IP addresses and ports, and are a handle to a communication chain. Applications typically make requests to the network through sockets or answer network Requests.
2, Socket's Personal understanding: socket as a special kind of file, some socket function is the operation of it (read/write io, open, Close)

second, Socket Type

Socket.af_unix can only be used for single UNIX system interprocess communication
Socket.af_inet network communication between servers
Socket.af_inet6ipv6
Socket. Sock_stream Streaming Socket,for TCP
Socket. Sock_dgram Datagram-type socket,for UDP
Socket. Sock_raw the original socket, the ordinary socket can not process ICMP,IGMP and other network messages, and Sock_raw may, second, Sock_raw can also handle special IPv4 messages, in addition, using the original socket, you can use the IP_ HDRINCL socket option constructed by User IP header
Socket. SOCK_RDM is a reliable form of UDP that guarantees delivery of datagrams but does not guarantee Data. 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


third, Socket Function Library

1. S.bind (address) binds sockets to Addresses. The format of address addresses depends on the address family. Under af_inet, address is represented as a tuple (host,port).
2, S.listen (backlog) began to listen for incoming connections. The backlog specifies the maximum number of connections that the operating system can suspend before rejecting the Connection. This value is at least 1, and most applications are set to 5.
3. S.connect (address) is connected to the socket at Address. In general, the format of address is a tuple (hostname,port), and if you connect to a server on the same machine, you can set hostname to "localhost". If the connection is faulted, a socket.error error is Returned.
4, s.connect_ex (address) function is the same as connect (address), but successfully returned 0, failed to return errno value
5, s.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
6, s.close () Close the Socket.
7, S.fileno () Returns the file descriptor of the socket
8, s.getpeername () Returns the remote address of the connection Socket. The return value is typically a tuple (ipaddr,port)
9. S.getsockname () Returns the Socket's own Address. Typically a tuple (ipaddr,port)
10, s.getsockopt (level,optname[,buflen]) Returns the value of the socket option
11, s.gettimeout () Returns the value of the current time period, in seconds, or none if no timeout period is set
12, S.RECV (bufsize[,flag]) accepts socket Data. The data is returned as a string, bufsize specifies the maximum amount of data to Receive. Flag provides additional information about the message, which can often be ignored
13, S.recvfrom (bufsize[,flag]) is similar to recv (), but the return value is (data,address). Where data is the string that contains the received information, and address is the socket that sends the data
15, S.send (string[,flag]) sends the data in the string to the connected Socket. The return value is the number of bytes to send, which may be less than the byte size of the string
16, S.sendall (string[,flag]) sends the data in the string to the connected socket, but attempts to send the data before it is Returned. Successful return none, failure throws exception
17, s.sendto (string[,flag],address) sends the data to the socket, address is a tuple in the form of (ipaddr,port), specifies the remote ADDRESS. The return value is the number of bytes Sent. This function is mainly used for UDP Protocol.
18, s.setblocking (flag) If flag is 0, the socket is set to non-blocking mode, otherwise the socket is set to block mode (the default value). In nonblocking mode, if the call recv () does not find any data, or the Send () call cannot send the data immediately, The Socket.error exception is caused
19, s.setsockopt (level,optname,value) Sets the value of the given socket option
20. S.settimeout (timeout) Sets the timeout period for the socket operation, and Timeou is a floating-point number in SECONDS. A value of none means that there are no super-periods, in general, the hyper-period should be set when the socket is just created, because they may be used for connected operations (connect ()) functions of a normal non-socket instance
1, getdefaulttimeout () Returns the default socket time-out (in seconds). None indicates that no time-outs are set
2. gethostbyname (hostname) converts the hostname (such as "www.baidu.com") to the IPV4 address, and the IP address is returned as a string, such as "8.8.8.8". IPV6 not supported
3, gethostname () Returns the host name of the Local machine

Python Socket class Library T Introduction

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.