Python---socket module

Source: Internet
Author: User
Tags fully qualified domain name

Just to commemorate the learning process! Translation and all aspects will be not in place. If there is an error, please indicate. Thank you.

#Coding:utf-8ImportSocket"""the socket () method returns a Socket object. """defcreate_connection (address,timeout,source_address):""". Connect a TCP service that listens for Internet addresses (Host,port) and returns the socket object. This is compared to the Socket.connect () Advanced method: If hosts is a non-numeric hostname, it will connect to all possible hosts until the connection is successful.    Makes it easy to write clients that are compatible with IPV4 and IPV6. Timeout sets the time-out for the socket instance before attempting to create the connection. If there is no time-out, the default is set to the global default time-out.    By: Getdefaulttimeout () to get. """Addrinfo=socket.getaddrinfo ("example.org", 80) [0][-1]    PrintAddrinfo Conn=socket.create_connection (addrinfo)PrintConndefgetaddrinfo (host,port,family,socktype,proto,flags):"""Convert the host/port to create a sequence of 5-element ancestors that are required to connect to the socket of that service that contains the necessary parameters.    Host is a domain name, a IPv4 or IPv6 string, or none. Port is a string service name similar to: "http", digitized port or none. For Host/port is none, it translates to null family, SocketType, Proto in the underlying C: is an optional parameter that is used to filter the return value. By default, they have a value of 0.    means that all the results are selected. Flags: You can determine how the results are calculated and returned.    The default value of flags is 0. For example: Ai_numerichost will not support the domain name, or will be error. Result is a $5 tuple in the following format: (family,socktype,proto,canonname,sockaddr) Family,socktype,proto are integers. will be converted to the socket () method. If Ai_canonname is one of the flags.            Canonname will represent the string canonname the host. Otherwise the canonname will be empty.            Sockadd is a ganso that describes the socket address. If it is af_inet, the format is: (Address,port), and if Af_inet6, return (address,port,flow info,scope ID) Example:"""    PrintSocket.getaddrinfo ("example.org", 80, 0,0,socket. IPPROTO_TCP)PrintSocket.getaddrinfo ("www.baidu.com","http")#Port can be "http"    PrintSocket.getaddrinfo ("www.baidu.com"," the")            defGetfqdn (name):""". Returns a fully qualified domain name corresponding to the name. If name is ignored, it will be interpreted as a local host.            In order to find a qualified domain name, the host name returned by GETHOSTBYADDR () is checked, along with the alias that comes with it. If available, the first name will be selected. When no qualified domain name is available, the return value of GetHostName () will be returned as the return value"""    PrintSocket.getfqdn ()PrintSocket.getfqdn ("www.baidu.com")    PrintSocket.getfqdn ("example.org")    defgethostbyname (hostname):"""Convert hostname to a string in IPV4 format. For example: 100.50.200.5.             If hostname itself is a string in IPv4 format, the original value is returned.    To see a more complete interface: GETHOSTBYNAME_EX (). gethostbyname () does not support IPV6. Getaddrinfo () should be used instead of IPV4/V6 dual support"""    PrintSocket.gethostbyname ("localhost")    PrintSocket.gethostbyname ("example.org")    PrintSocket.gethostbyname ("www.baidu.com")    PrintSocket.gethostbyname ("10.2.3.6")defGETHOSTBYNAME_EX (hostname):""". Convert hostname to IPV4 format.            is an extension interface for gethostbyname (hostname).    Returns a ternary group (hostname,aliaslist,ipaddrlist) here: hostname is the given original hostname of the corresponding IPAddress. Aliaslist "may be empty" is the list of host names for the same address to choose from.    Ipaddrlist is the IPv4 address list corresponding to the same interface for the same host. GETHOSTBYNAME_EX () does not support IPv6 addresses. Getaddrinfo () should be used to provide support for IPv4 and IPv6"""    PrintSOCKET.GETHOSTBYNAME_EX ("localhost")    PrintSOCKET.GETHOSTBYNAME_EX ("example.org")    PrintSOCKET.GETHOSTBYNAME_EX ("www.baidu.com")    PrintSOCKET.GETHOSTBYNAME_EX ("127.0.0.1")defgethostname ():"""returns a string that is executing the host name of the Python interpreter if you want to know the current host IP, you can use: gethostbyname (GetHostName ()). This operation assumes that there is a valid IP address and a host mapping.            This hypothesis is not always valid. Note: gethostname () does not always return a fully-sized domain name. should use GETFQDN ()"""    PrintSocket.gethostname ()PrintSocket.gethostbyname (Socket.gethostname ())PrintSocket.getfqdn ()"recommended use of Socket.getfqdn ()"    PrintSocket.gethostbyname (Socket.getfqdn ())defgethostbyaddr (ip_address):"""return Triples: (hostname,aliaslist,ipaddrlist) The results are consistent with GETHOSTBYNAME_EX (hostname).    To get a fully-sized hostname, use: Getfqdn ().    Gethostbyaddress () is supported for both IPv4 and IPv6. """    PrintSOCKET.GETHOSTBYADDR ("127.0.0.1")defGetnameinfo (sockaddr,flags):"""convert sockaddr to 2 progenitor: (Host,port).            The flags determine whether the result contains a full-size domain name or a digitized address to the host.    Similarly, port can contain either a string port name or a number-type end number. """sockaddr=socket.getaddrinfo ("127.0.0.1"," -") [0][4]    PrintSocket.getnameinfo (Sockaddr,socket. Ai_numerichost)defGetprotobyname (protocolname):"""The network protocol name is converted to a constant that is suitable for delivery. Just like the third optional parameter in the socket () function.            Only the socket that will be opened in Sock_raw mode needs to be. For normal socket mode, if the protocol is 0 or is ignored, the correct protocol will be automatically selected"""    PrintSocket.getprotobyname ("ICMP")#1    PrintSocket.getprotobyname ("UDP")# -    PrintSocket.getprotobyname ("TCP")# -    PrintSocket.getprotobyname ("IP")#0    defGetservbyname (servicename,protocolname):"""Convert the Network service name and protocol name to the port number for this service. The Optional Protocol name is: TCP UDP or any other protocol that can match ProtocolName is an optional parameter"""defGetservbyport (port,protocolname):"""Convert the network port number and protocol name to the service name of that service. ProtocolName is similar to: "TCP" upd, such as a matching protocol """"defsocket_test (Family,type,proto):"""family, type, proto are optional to create a socket.family with the given address family, socket type, and protocol number default is Af_inet or Af_inet6 or Af_unix socket class The default type is: Sock_stream. Sock_dgram or other Sock_ ...    One of the constants.  Proto is 1 or omitted ... 1 O'Clock is the "IP" protocol"""    PrintSocket.socket ()defSocketpair (Family,type,proto):"""returns a pair of connected socket objects created using Famil, type, Proto.  The family, type, proto, and socket () parameters are consistent by default family to Af_inet.            If rejected by the platform, family defaults to Af_unix. This method applies to the platform: UNIX"""    #print Socket.socketpair () #windows上没有该方法   defFROMFD (Fd,family,type,proto):"""copies the file descriptor (the integer returned by File.fileno ()) and creates a socket object from the result. """    defntoh1 (x):"""Convert positive integers of 32bit on the network to host byte order. When the host byte order and network byte order are consistent, there is no action. Otherwise, perform a 4bit conversion operation"""    defNtohs (x):"""Convert positive integers of 16bit on the network to host byte order. When the host byte order and network byte order are consistent, there is no action. Otherwise, perform a 2bit conversion operation"""    defhtonl (x):"Convert a positive integer of 32bit on this machine to the network byte order. When the host byte order and network byte order are consistent, there is no action. Otherwise, perform a 4bit conversion operation"defhtons (x):"Convert a positive integer of 16bit on the host to the host byte order. When the host byte order and network byte order are consistent, there is no action. Otherwise, perform a 2bit conversion operation"defInet_atoon (ip_string):"""        """if __name__=="__main__":    """create_connection (None,none,none) getaddrinfo (none,none,none,none,none,none) Getfqdn (None) gethostbyname (    None) gethostbyname_ex (None) GetHostName () gethostbyaddr (none) Getnameinfo (None,none) getprotobyname (none) Getservbyname (None,none) socket_test (none,none,none)"""                                          

to_be_continued!

Python---socket module

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.