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).




本文转自 煮酒品茶 51CTO博客,原文链接:http://blog.51cto.com/cwtea/1208090,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
开发者 Python
Python Socket编程:不只是基础,更有进阶秘籍,让你的网络应用飞起来!
在数字时代,网络应用成为连接世界的桥梁。Python凭借简洁的语法和丰富的库支持,成为开发高效网络应用的首选。本文通过实时聊天室案例,介绍Python Socket编程的基础与进阶技巧。基础篇涵盖服务器和客户端的建立与数据交换;进阶篇则探讨多线程与异步IO优化方案,助力提升应用性能。通过本案例,你将掌握Socket编程的核心技能,推动网络应用飞得更高、更远。
69 1
|
1月前
|
Python
Python编程中的魔法方法(Magic Methods)
【10月更文挑战第40天】在Python的世界中,魔法方法就像是隐藏在代码背后的神秘力量。它们通常以双下划线开头和结尾,比如 `__init__` 或 `__str__`。这些方法定义了对象的行为,当特定操作发生时自动调用。本文将揭开这些魔法方法的面纱,通过实际例子展示如何利用它们来增强你的类功能。
15 1
|
2月前
|
Python
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
使用Python的socket库实现客户端到服务器端的图片传输,包括客户端和服务器端的代码实现,以及传输结果的展示。
159 3
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
|
2月前
|
JSON 数据格式 Python
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
本文介绍了如何使用Python的socket模块实现客户端到服务器端的文件传输,包括客户端发送文件信息和内容,服务器端接收并保存文件的完整过程。
173 1
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
|
1月前
|
Kubernetes 网络协议 Python
Python网络编程:从Socket到Web应用
在信息时代,网络编程是软件开发的重要组成部分。Python作为多用途编程语言,提供了从Socket编程到Web应用开发的强大支持。本文将从基础的Socket编程入手,逐步深入到复杂的Web应用开发,涵盖Flask、Django等框架的应用,以及异步Web编程和微服务架构。通过本文,读者将全面了解Python在网络编程领域的应用。
33 1
|
2月前
|
消息中间件 监控 网络协议
Python中的Socket魔法:如何利用socket模块构建强大的网络通信
本文介绍了Python的`socket`模块,讲解了其基本概念、语法和使用方法。通过简单的TCP服务器和客户端示例,展示了如何创建、绑定、监听、接受连接及发送/接收数据。进一步探讨了多用户聊天室的实现,并介绍了非阻塞IO和多路复用技术以提高并发处理能力。最后,讨论了`socket`模块在现代网络编程中的应用及其与其他通信方式的关系。
226 3
|
2月前
|
安全 网络协议 网络安全
Python Socket编程大揭秘:从菜鸟到黑客的进阶之路,你准备好了吗?
【10月更文挑战第4天】在编程领域,Python Socket编程犹如一把开启网络世界的钥匙,带领开发者从简单数据传输迈向复杂应用构建。本文将引导你从零开始,逐步掌握Socket编程的核心技巧,包括基本概念、TCP服务器与客户端的搭建、并发处理及异常管理、SSL/TLS加密通信,直至深入了解网络协议与安全漏洞。通过实战演练与理论学习,助你成为驾驭网络世界的高手。
36 1
|
2月前
|
存储 网络协议 Linux
聊一聊 Python 的 socket,以及 select、poll、epoll 又是怎么一回事?
聊一聊 Python 的 socket,以及 select、poll、epoll 又是怎么一回事?
159 2
|
3月前
|
网络协议 Python
网络世界的建筑师:Python Socket编程基础与进阶,构建你的网络帝国!
在数字宇宙中,网络如同复杂脉络连接每个角落,Python Socket编程则是开启这一世界的钥匙。本文将引导你从基础概念入手,逐步掌握Socket编程,并通过实战示例构建TCP/UDP服务器与客户端。你将学会使用Python的socket模块进行网络通信,了解TCP与UDP的区别,并运用多线程与异步IO提升服务器性能。跟随本文指引,成为网络世界的建筑师,构建自己的网络帝国。
39 2
|
3月前
|
网络协议 Python
告别网络编程迷雾!Python Socket编程基础与实战,让你秒变网络达人!
在网络编程的世界里,Socket编程是连接数据与服务的关键桥梁。对于初学者,这往往是最棘手的部分。本文将用Python带你轻松入门Socket编程,从创建TCP服务器与客户端的基础搭建,到处理并发连接的实战技巧,逐步揭开网络编程的神秘面纱。通过具体的代码示例,我们将掌握Socket的基本概念与操作,让你成为网络编程的高手。无论是简单的数据传输还是复杂的并发处理,Python都能助你一臂之力。希望这篇文章成为你网络编程旅程的良好开端。
65 3