设为首页 收藏本站
查看: 1426|回复: 0

[经验分享] Python socket.help Methods

[复制链接]

尚未签到

发表于 2018-8-4 10:11:15 | 显示全部楼层 |阅读模式
DESCRIPTION
  
This module provides socket operations and some>  
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 =>  
    |  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>  
    #在当前返回一个新的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).

  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-546323-1-1.html 上篇帖子: Python任务调度之sched 下篇帖子: python wx 的wx.Frame框架属性
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表