Python socket.help Methods

DESCRIPTION
    This module provides socket operations and some related functions.
    On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
    On other systems, it only supports IP. Functions specific for a
    socket are available as methods of the socket object.
    #此模块提供了socket操作和一些相关的功能。
    #在Unix上,它支持IP(互联网协议)和Unix域sockets。
    #在其他系统上,它仅支持IP。一个特定的功能
    #插座套接字对象的方法。
    Functions:
#创建一个新的对象
    socket() -- create a new socket object
#创建一对新的对象
    socketpair() -- create a pair of new socket objects [*]
#从一个打开的文件描述符中创建一个socket对象
    fromfd() -- create a socket object from an open file descriptor [*]
#返回当前的主机名
    gethostname() -- return the current hostname
#I获取主机名的IP地址,必须传一个参数
    gethostbyname() -- map a hostname to its IP number
#IP地址或者主机的dns信息,必须传一个参数
    gethostbyaddr() -- map an IP number or hostname to DNS info
#返回给定服务名和协议的端口号
    getservbyname() -- map a service name and a protocol name to a port number
exp:
>>> print socket.getservbyname("ftp")
21
>>> print socket.getservbyname("http")
80
>>>
#协议名称的数量?
    getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
exp:
>>> print socket.getprotobyname("tcp")
6
#从网络主机转换16/32的字节顺序
    ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
    htons(), htonl() -- convert 16, 32 bit int from host to network byte order
    inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
#32-bit的包格式转换成字符串(123.45.67.89)
    inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
#安全socket sll
    ssl() -- secure socket layer support (only available if configured)
#获取默认的超时值
    socket.getdefaulttimeout() -- get the default timeout value
#设置默认的超时值,超时后程序自毁
    socket.setdefaulttimeout() -- set the default timeout value
#连接到一个地址,可选的一个超时值
    create_connection() -- connects to an address, with an optional timeout
     [*] not available on all platforms!
    Special objects:
    SocketType -- type object for socket objects
    error -- exception raised for I/O errors
    has_ipv6 -- boolean value indicating if IPv6 is supported
    Integer constants:
    AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
    SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
    Many other constants may be defined; these may be used in calls to
    the setsockopt() and getsockopt() methods.
CLASSES
    __builtin__.object
        _socketobject
        _socketobject
    exceptions.IOError(exceptions.EnvironmentError)
        error
            gaierror
            herror
            timeout
    SocketType = class _socketobject(__builtin__.object)
     |  socket([family[, type[, proto]]]) -> socket object
     | 
    #打开一个 给定类型的socket.family指定地址族,他默认是AF_INET,type类型
是一个流,默认SOCK_STREAM(tcp),或者数据流SOCK_DGRAM(udp),这个protocol
默认参数是0,关键字参数都可以接收。
     |  Open a socket of the given type.  The family argument specifies the
     |  address family; it defaults to AF_INET.  The type argument specifies
     |  whether this is a stream (SOCK_STREAM, this is the default)
     |  or datagram (SOCK_DGRAM) socket.  The protocol argument defaults to 0,
     |  specifying the default protocol.  Keyword arguments are accepted.
     | 
#一个socket对象代表一个网络连接
     |  A socket object represents one endpoint of a network connection.
     |  #socket对象的方法(关键字参数不允许)
     |  Methods of socket objects (keyword arguments not allowed):
     |  accept() -- accept a connection, returning new socket and client address
#接受一个连接,返回一个新的socket和客户端地址。
exp:
print出来一个二元元组,一般进行拆分
(<socket._socketobject object at 0xb7791d84>, ('127.0.0.1', 48336))
     |  bind(addr) -- bind the socket to a local address
#绑定一个socket到本地地址
     |  close() -- close the socket
    #关闭socket链接
     |  connect(addr) -- connect the socket to a remote address
#socket连接到远程地址
     |  connect_ex(addr) -- connect, return an error code instead of an exception
#连接,返回一个错误代码而不是异常。
exp:
>>> s.connect_ex(("www.baidu.com",80))
0
>>> s.connect_ex(("www.caokbkbkbkbkkbkb.com",80))
106
>>> s.connect_ex(("www.caokbkbkb2222221.com",80))
106
     |  dup() -- return a new socket object identical to the current one [*]
    #在当前返回一个新的socket对象
     |  fileno() -- return underlying file descriptor
    # 返回低层的文件描述符
>>> print s.fileno()
3
     |  getpeername() -- return remote address [*]
#返回远程地址
     |  getsockname() -- return local address
>>> print s.getpeername() 刚返回的是百度的
        ('61.135.169.125', 80)
     |  getsockopt(level, optname[, buflen]) -- get socket options
    #获取socket选项
     |  gettimeout() -- return timeout or None
#返回timeout()超时值
     |  listen(n) -- start listening for incoming connections
    #启动监听传入的连接
     |  makefile([mode, [bufsize]]) -- return a file object for the socket [*]
#返回一个socket的文件对象
     |  recv(buflen[, flags]) -- receive data
#接收数据
     |  recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)
#接收数据,信息来自缓冲区
     |  recvfrom(buflen[, flags]) -- receive data and sender's address
#输出数据和发送者的地址
     |  recvfrom_into(buffer[, nbytes, [, flags])
     |    -- receive data and sender's address (into a buffer)
#输出数据和发送者的地址,信息来自缓冲区
     |  sendall(data[, flags]) -- send all data
#发送所有数据。
     |  send(data[, flags]) -- send data, may not send all of it
#发送数据时,可能不会发送所有
     |  sendto(data[, flags], addr) -- send data to a given address
#将数据发送给一个指定的地址
     |  setblocking(0 | 1) -- set or clear the blocking I/O flag
#设置或清除阻塞IO标志
     |  setsockopt(level, optname, value) -- set socket options
#设置socket选项
     |  settimeout(None | float) -- set or clear the timeout
    #设置或清除超时值
     |  shutdown(how) -- shut down traffic in one or both directions
     |  #关闭流量在进或出?
     |   [*] not available on all platforms!
     | 
#定义方法
     |  Methods defined here:
     | 
     |  __init__(self, family=2, type=1, proto=0, _sock=None)
     | 
     |  accept(self)
     |      accept() -> (socket object, address info)
     |     
     |      Wait for an incoming connection.  Return a new socket representing the
     |      connection, and the address of the client.  For IP sockets, the address
     |      info is a pair (hostaddr, port).
     | 
     |  bind(self, *args)
     |      bind(address)
     |     
     |      Bind the socket to a local address.  For IP sockets, the address is a
     |      pair (host, port); the host must refer to the local host. For raw packet
     |      sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
     | 
     |  close(self)
     |      close()
     |     
     |      Close the socket.  It cannot be used after this call.
     | 
     |  connect(self, *args)
     |      connect(address)
     |     
     |      Connect the socket to a remote address.  For IP sockets, the address
     |      is a pair (host, port).
     |  #返回一个整型
     |  connect_ex(self, *args)
     |      connect_ex(address) -> errno
exp:
import socket
s = socket.socket()
ConnError = s.connect_ex(("www.baidu.com",80))
if ConnError == 0:
        print "connect is ok"
     |     
     |      This is like connect(address), but returns an error code (the errno value)
     |      instead of raising an exception when an error occurs.
     | 
     |  dup(self)
     |      dup() -> socket object
     |     
     |      Return a new socket object connected to the same system resource.
     | 
     |  fileno(self, *args)
     |      fileno() -> integer
     |     
     |      Return the integer file descriptor of the socket.
     | 
     |  getpeername(self, *args)
     |      getpeername() -> address info
     |     
     |      Return the address of the remote endpoint.  For IP sockets, the address
     |      info is a pair (hostaddr, port).
     | 
     |  getsockname(self, *args)
     |      getsockname() -> address info
     |     
     |      Return the address of the local endpoint.  For IP sockets, the address
     |      info is a pair (hostaddr, port).
     | 
     |  getsockopt(self, *args)
     |      getsockopt(level, option[, buffersize]) -> value
     |     
     |      Get a socket option.  See the Unix manual for level and option.
     |      If a nonzero buffersize argument is given, the return value is a
     |      string of that length; otherwise it is an integer.
     | 
     |  gettimeout(self, *args)
     |      gettimeout() -> timeout
     |     
     |      Returns the timeout in floating seconds associated with socket
     |      operations. A timeout of None indicates that timeouts on socket
     |      operations are disabled.
     | 
     |  listen(self, *args)
     |      listen(backlog)
     |     
     |      Enable a server to accept connections.  The backlog argument must be at
     |      least 1; it specifies the number of unaccepted connection that the system
     |      will allow before refusing new connections.
     | 
     |  makefile(self, mode='r', bufsize=-1)
     |      makefile([mode[, bufsize]]) -> file object
     |     
     |      Return a regular file object corresponding to the socket.  The mode
     |      and bufsize arguments are as for the built-in open() function.
     | 
     |  sendall(self, *args)
     |      sendall(data[, flags])
     |     
     |      Send a data string to the socket.  For the optional flags
     |      argument, see the Unix manual.  This calls send() repeatedly
     |      until all data is sent.  If an error occurs, it's impossible
     |      to tell how much data has been sent.
     | 
     |  setblocking(self, *args)
     |      setblocking(flag)
     |     
     |      Set the socket to blocking (flag is true) or non-blocking (false).
     |      setblocking(True) is equivalent to settimeout(None);
     |      setblocking(False) is equivalent to settimeout(0.0).
     | 
     |  setsockopt(self, *args)
     |      setsockopt(level, option, value)
     |     
     |      Set a socket option.  See the Unix manual for level and option.
     |      The value argument can either be an integer or a string.
     | 
     |  settimeout(self, *args)
     |      settimeout(timeout)
     |     
     |      Set a timeout on socket operations.  'timeout' can be a float,
     |      giving in seconds, or None.  Setting a timeout of None disables
     |      the timeout feature and is equivalent to setblocking(1).
     |      Setting a timeout of zero is the same as setblocking(0).
     | 
     |  shutdown(self, *args)
     |      shutdown(flag)
     |     
     |      Shut down the reading side of the socket (flag == SHUT_RD), the writing side
     |      of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).

    原文作者:煮酒品茶
    原文地址: https://blog.51cto.com/cwtea/1208090
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞