Python socket.help問題

來源:互聯網
上載者:User
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 numberexp:>>> print socket.getservbyname("ftp")21>>> print socket.getservbyname("http")80>>>#協議名稱的數量?    getprotobyname() -- map a protocol name (e.g. 'tcp') to a numberexp:>>> 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.bianceng.cn",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) -> errnoexp:import sockets = socket.socket()ConnError = s.connect_ex(("www.bianceng.cn",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).

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.