广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python socket.help M
  • 496
分享到

Python socket.help M

Pythonsocket 2023-01-31 07:01:37 496人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

DESCRIPTioN This module provides Socket operations and some related functions. On Unix, it supports IP

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


--结束END--

本文标题: Python socket.help M

本文链接: https://www.lsjlt.com/news/191939.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python socket.help M
    DESCRIPTION This module provides socket operations and some related functions. On Unix, it supports IP...
    99+
    2023-01-31
    Python socket
  • 用python -m http.serv
    工作时同事间几mb小文件的传输,一般使用QQ或者微信就足够了,但当传输文件几百MB或者几十G时,这种方法的效率就显得不足了。本篇就是简单说明一个python小功能,让大家能利用python方便的搭建一个本地局域网。跟同事测试时,速...
    99+
    2023-01-31
    python http serv
  • Python的-m参数
    1、在命令行中启动Python脚本的时候,经常会用到-m参数,那么-m起了什么作用呢?   2、先看看 python --help 给出的信息: run library module as a script (terminat...
    99+
    2023-01-31
    参数 Python
  • [Python]Python操作/管理M
    先确定环境是否已支持MySQLdb模块,如果没有,请安装,如下: [root@bw-vm-soft ~]# wget http://jaist.dl.sourceforge.net/project/mysql-python/mys...
    99+
    2023-01-31
    操作 Python
  • 充分理解 python -m mod
    最近在看 __main__ 的官方文档 —— https://docs.python.org/3/library/__main__.html#module-__main__,提到一个 python -m 的用法,很是不理解,所以查找了很多...
    99+
    2023-01-31
    python mod
  • python 使用pymssql 连接M
    知识点:如果连接数据库不使用默认端口,需要在连接host地址上加上端口 如cacelbert01.mysql.alibabalabs.com:3306 #coding=gbk ################################...
    99+
    2023-01-31
    python pymssql
  • Python自动化运维:Django M
    QuerySet可切片使用Python 的切片语法来限制查询集记录的数目 。它等同于SQL 的LIMIT 和OFFSET 子句。>>> Entry.objects.all()[:5]      # (LIMIT 5) &g...
    99+
    2023-01-31
    Python Django
  • 【Python】python -m pip install 和 pip install 的区别
    文章目录 一、pip install二、python -m pip install三、两者的总结 一、pip install 当你使用 pip install 命令时,你正在使用 Py...
    99+
    2023-09-01
    python pip 人工智能 原力计划
  • 【framework】spring3-m
    前言 spring一直以来提供了大量文档和例子,来让我们熟悉和了解spring. springMVC是一个比较成功的MVC模式,有人甚至认为这是java最好的web开发模式。 这个,我们不评价,不过这也能说明一些问题。 自spring3加入...
    99+
    2023-01-31
    framework
  • python3.6+scrapy+m
    最近闲着,把之前写的小爬虫分享一下,才疏学浅,仅当参考。 [介绍文档] python版本:python3.6 scrapy: 1.5.0 需要安装pymysql包支持访问mysql数据库 可以使用pip安装...
    99+
    2023-01-31
    scrapy
  • python 报错python.exe -m pip install --upgrade pip‘ command.
    WARNING: You are using pip version 21.1.1; however, version 22.3.1 is available. You should consider upgrading via the ...
    99+
    2023-09-29
    python Powered by 金山文档
  • 关于CentOS下python无法安装m
    说明,我CentOS的python是2.7版本   easy_install MySQL-python 提示报错说少了mysql.c等相关文件。   处理方式很简单: yum install mysql-devel   然后重新 easy_...
    99+
    2023-01-31
    CentOS python
  • 外部python程序调用django的m
    查了几篇文章,结合了一下,以下是使用范例:我的工程是/www/web/cmdb/我的models文件在/www/web/cmdb/serverpwd/models.pydjango的settings文件在/www/web/cmdb/cmdb...
    99+
    2023-01-31
    程序 python django
  • python-将爬取到的m3u8合并为m
    当你看到这个博客的时候恭喜你,你以后不用开vip就可以观看和下载vip视频了 最简单的观看vip视频步骤:进入全民解析网将vip视频地址进行解析   以下代码是通过python将vip视频进行下载为mp4格式 步骤及其代码 先找到要爬取的...
    99+
    2023-01-30
    并为 python 将爬取到
  • 怎么部署mysql双向主从复制M-M
    本篇内容介绍了“怎么部署mysql双向主从复制M-M”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!通过my...
    99+
    2022-10-18
  • 详解MySQL数据类型int(M)中M的含义
    介绍 MySQL 数据类型中的 integer types 有点奇怪。你可能会见到诸如:int(3)、int(4)、int(8) 之类的 int 数据类型。刚接触 MySQL 的时候,我还以为 int(3)...
    99+
    2022-06-04
    详解 数据类型 含义
  • 使用python cgi上传文件并计算m
    对文件拷贝后进行MD5值比较,看是不是拷贝完全。google和baidu上都是使用md5模块读取所有的文件进内存,在计算md5,导致计算超过1G大小的文件报错。增量计算MD5的方法:#!/usr/bin/python import hash...
    99+
    2023-01-31
    上传文件 python cgi
  • Python一点通: ‘python -m pip install’ 和 ‘pip install‘ 什么区别?
    在 Python 中安装包可以使用包管理器 pip。有两种方法运行 pip 来安装包:python -m pip install 和 pip install。在本文中,我们将讨论这两者的区别。 pyt...
    99+
    2023-09-15
    python pip 人工智能
  • Turning Vim into a m
    http://sontek.net/blog/detail/turning-vim-into-a-modern-python-ideContentsIntroBasic Editing and DebuggingCode FoldingWi...
    99+
    2023-01-31
    Turning Vim
  • openstack M版本部署
    系统解决方案 一、环境需求1、网卡em1em2em3em4controller1172.16.16.1172.16.17.1nonenonecontroller1172.16.16.2172.16...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作