Python socket.help问题

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.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) -> errno
exp:
import socket
s = 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).

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索timeout
, socket
, object
, sockets
, address
, floating ip
, 一个
, The
getservbyname
python help、python help 怎么用、python help 怎么退出、python help文档、python退出help,以便于您获取更多的相关知识。

时间: 2024-11-01 03:46:29

Python socket.help问题的相关文章

Python socket编程实例详解

  本文实例形式较为详细的讲述了Python socket编程.分享给大家供大家参考.具体如下: 复制代码 代码如下: sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 上面的代码创建了一个socket对象.type参数代表套接字类型,可为SOCK_STREAM(流套接字)和SOCK_DGRAM(数据报套接字).AF_INET表示创建的是ip v4的类型. 复制代码 代码如下: socket(address_family,type,pro

Python Socket通讯程序例子

python中内置的socket模块使得网络编程更加简单化,下面就通过两个小小脚本来了解客户端如何与服务器端建立socket. 客户端代码: #clietn.py if __name__ == '__main__':  #判断是否调用自己本身,如果不是则__name__为脚本名称 import socket  #导入我们所需的socket模块sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #第一步是创建socket对象.调用so

python学习-Python socket 聊天器

问题描述 Python socket 聊天器 用python做一个多用户的聊天器.就是指一个服务器,然后不同的电脑上开启客户端,都能连接,还可以互相传递消息.但是现在的问题是都能连接上,但相互之间不能通信.一个连接上后就把原来连接的挤掉了.该怎么解决? 解决方案 你没写端口吧,明显通讯拥挤 解决方案二: 客户端都是连接到服务器,那么数据应该是通过服务器来中转 解决方案三: python socket聊天小工具

Python Socket 网络编程

原文:Python Socket 网络编程 Socket 是进程间通信的一种方式,它与其他进程间通信的一个主要不同是:它能实现不同主机间的进程间通信,我们网络上各种各样的服务大多都是基于 Socket 来完成通信的,例如我们每天浏览网页.QQ 聊天.收发 email 等等.要解决网络上两台主机之间的进程通信问题,首先要唯一标识该进程,在 TCP/IP 网络协议中,就是通过 (IP地址,协议,端口号) 三元组来标识进程的,解决了进程标识问题,就有了通信的基础了. 本文主要介绍使用 Python 进

Python Socket 编程——聊天室示例程序

原文:Python Socket 编程--聊天室示例程序 上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和客户端的代码了解基本的 Python Socket 编程模型.本文再通过一个例子来加强一下对 Socket 编程的理解. 聊天室程序需求 我们要实现的是简单的聊天室的例子,就是允许多个人同时一起聊天,每个人发送的消息所有人都能接收到,类似于 QQ 群的功能,而不是点对点的 QQ 好友之间的聊天.如下图: 图来自:http://www.ibm.com/de

有关python socket 多线程问题

问题描述 有关python socket 多线程问题 我现在写的程序是模拟p2p DHT 就是peer1 知道peer2 和 peer3的port 依此类推 现在我写了一个程序 打开的每个xterm端口是一个peer 窗口里可以显示ping所返回的信息代码如下我测试了好久 有的显示返回信息 有的显示接收信息 有的豆显示 有的根本不显示刚学编程半年 python很多东西不了解 socket 和多线程完全是先学先用的 所以请各位看一下到底问题出在哪里 以下是代码 import sysimport t

server-java socket客户端和Python socket客户端的不同?高C币!!

问题描述 java socket客户端和Python socket客户端的不同?高C币!! 我们接的项目有一个java写的server,我只有它的文档,并且用java已经实现,但是现在要为 python实现,用java实现的代码: public static void main(String[] args) throws WindException, SendException, RecvException{ String host="172.22.128.16"; int port=

【Python之旅】第五篇(一):Python Socket通信原理

 只要和网络服务涉及的,就离不开Socket以及Socket编程,下面就说说Python Socket通信的基本原理. 1.Socket     socket也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求或者应答网络请求.可以列举中国移动或者是中国电信等的电话客服,当然,也可以看下面的图片来作形象的说明.     socket起源于Unix,而Unix/Linux基本哲学之一就是:一切皆文件,即都可以用&quo

Python Socket Client及Server简单编程

本文主要介绍使用Python语言编写Socket协议Server及Client的简单实现方法. 1. Python Socket编程简介 Socket通常也称作"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 三种流行的套接字类型是:stream,datagram和raw.stream和datagram套接字可以直接与TCP协议进行接口,而raw套接字则接口到IP协议. Python Socket模块提供了对低层BSD套接字样式网络的访问,使用

【Python之旅】第五篇(三):Python Socket多线程并发

 前面的几个例子都是单线程的,下面再来说说多线程的. 1.多线程模块     主要是socketserver模块,如下图示: 2.多线程原理     如下图示说明: 3.SockteServer例子说明 服务器端: 客户端: 4.演示     还是以前面例子,对代码进行修改,作如下的演示. Server端: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import SocketServer            #导入SocketServer,