Python成长笔记 - 基础篇 (八)

socket编程
应用层:
表示层:
会话层:
传输层:
网络层: ip地址
数据链路层: MAC地址
物理层:
 
协议类型:
TCP/IP协议:三次握手,四次断开
 
 

2. Socket 参数介绍

 

socket.socket(family=AF_INETtype=SOCK_STREAMproto=0fileno=None)  必会

Create a new socket using the given address family, socket type and protocol number. The address family should be AF_INET (the default), AF_INET6AF_UNIXAF_CAN or AF_RDS. The socket type should beSOCK_STREAM (the default), SOCK_DGRAMSOCK_RAW or perhaps one of the other SOCK_ constants. The protocol number is usually zero and may be omitted or in the case where the address family is AF_CAN the protocol should be one of CAN_RAW or CAN_BCM. If fileno is specified, the other arguments are ignored, causing the socket with the specified file descriptor to return. Unlike socket.fromfd()fileno will return the same socket and not a duplicate. This may help close a detached socket using socket.close().html

socket.socketpair([family[, type[, proto]]])python

Build a pair of connected socket objects using the given address family, socket type, and protocol number. Address family, socket type, and protocol number are as for the socket() function above. The default family is AF_UNIX if defined on the platform; otherwise, the default is AF_INET.编程

socket.create_connection(address[, timeout[, source_address]])服务器

Connect to a TCP service listening on the Internet address (a 2-tuple (host, port)), and return the socket object. This is a higher-level function than socket.connect(): if host is a non-numeric hostname, it will try to resolve it for both AF_INET and AF_INET6, and then try to connect to all possible addresses in turn until a connection succeeds. This makes it easy to write clients that are compatible to both IPv4 and IPv6.网络

Passing the optional timeout parameter will set the timeout on the socket instance before attempting to connect. If no timeout is supplied, the global default timeout setting returned by getdefaulttimeout() is used.socket

If supplied, source_address must be a 2-tuple (host, port) for the socket to bind to as its source address before connecting. If host or port are ‘’ or 0 respectively the OS default behavior will be used.函数

socket.getaddrinfo(hostportfamily=0type=0proto=0flags=0) #获取要链接的对端主机地址 必会ui

sk.bind(address) 必会编码

  s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下,以元组(host,port)的形式表示地址。spa

sk.listen(backlog) 必会

  开始监听传入链接。backlog指定在拒绝链接以前,能够挂起的最大链接数量。

      backlog等于5,表示内核已经接到了链接请求,但服务器尚未调用accept进行处理的链接个数最大为5
      这个值不能无限大,由于要在内核中维护链接队列

sk.setblocking(bool) 必会

  是否阻塞(默认True),若是设置False,那么accept和recv时一旦无数据,则报错。

sk.accept() 必会

  接受链接并返回(conn,address),其中conn是新的套接字对象,能够用来接收和发送数据。address是链接客户端的地址。

  接收TCP 客户的链接(阻塞式)等待链接的到来

sk.connect(address) 必会

  链接到address处的套接字。通常,address的格式为元组(hostname,port),若是链接出错,返回socket.error错误。

sk.connect_ex(address)

  同上,只不过会有返回值,链接成功时返回 0 ,链接失败时候返回编码,例如:10061

sk.close() 必会

  关闭套接字

sk.recv(bufsize[,flag]) 必会

  接受套接字的数据。数据以字符串形式返回,bufsize指定最多能够接收的数量。flag提供有关消息的其余信息,一般能够忽略。

sk.recvfrom(bufsize[.flag])

  与recv()相似,但返回值是(data,address)。其中data是包含接收数据的字符串,address是发送数据的套接字地址。

sk.send(string[,flag]) 必会

  将string中的数据发送到链接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。即:可能未将指定内容所有发送。

sk.sendall(string[,flag]) 必会

  将string中的数据发送到链接的套接字,但在返回以前会尝试发送全部数据。成功返回None,失败则抛出异常。

      内部经过递归调用send,将全部内容发送出去。

sk.sendto(string[,flag],address)

  将数据发送到套接字,address是形式为(ipaddr,port)的元组,指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。

sk.settimeout(timeout) 必会

  设置套接字操做的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。通常,超时期应该在刚建立套接字时设置,由于它们可能用于链接的操做(如 client 链接最多等待5s )

sk.getpeername()  必会

  返回链接套接字的远程地址。返回值一般是元组(ipaddr,port)。

sk.getsockname() 

  返回套接字本身的地址。一般是一个元组(ipaddr,port)

sk.fileno() 

  套接字的文件描述符

socket.sendfile(fileoffset=0count=None)

     发送文件 ,但目前多数状况下并没有什么卵用。

相关文章
相关标签/搜索