码迷,mamicode.com
首页 > 其他好文 > 详细

socket

时间:2019-07-25 00:02:57      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:相关   for   return   encode   ipv4   lib   unix   NPU   exp   

socket

1.      socket

Source code: Lib/socket.py

This module provides access to the BSD socket interface. It is available on all modern Unix systems, Windows, MacOS, and probably additional platforms.

 

The Python interface is a straightforward transliteration of the Unix system call and library interface for sockets to Python’s object-oriented style: the socket() function returns a socket object whose methods implement the various socket system calls. Parameter types are somewhat higher-level than in the C interface: as with read() and write() operations on Python files, buffer allocation on receive operations is automatic, and buffer length is implicit on send operations.

简单来说,它只是对底层进行了简单的封装;返回一个socket对象,可以执行系统调用。

参数类别比C接口中的封装程序稍高一些:与文件的read()和write()操作一样,接收操作的缓冲区分配是自动的,而发送操作的缓冲区长度是隐式的。

 

1.1.    Socket families

Depending on the system and the build options, various socket families are supported by this module.

 

The address format required by a particular socket object is automatically selected based on the address family specified when the socket object was created.

可根据所给出的地址类型自动选择套接字类型。

常用地址类型如下:

 

socket families:

  AF_UNIX  unix本机之间进行通信

  AF_INET  使用IPv4

  AF_INET6  使用IPv6

socket types:

  SOCK_STREAM          # TCP套接字类型

  SOCK_DGRAM   # UDP套接字类型

  SOCK_RAW      #原始套接字类型,这个套接字比较强大,创建这种套接字可以监听网卡上的所有数据帧

  SOCK_RDM  #是一种可靠的UDP形式,即保证交付数据报但不保证顺序。SOCK_RAM用来提供对原始协议的低级访问,在需要执行某些特殊操作时使用,如发送ICMP报文。SOCK_RAM通常仅限于高级用户或管理员运行的程序使用。

其它具体可见相关文档。

 

1.2.    Functions

1.2.1.   Creating sockets

The following functions all create socket objects.

 

socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=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_INET6, AF_UNIX, AF_CAN, AF_PACKET, or AF_RDS. The socket type should be SOCK_STREAM (the default), SOCK_DGRAM, SOCK_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, CAN_BCM or CAN_ISOTP.

 

If fileno is specified, the values for family, type, and proto are auto-detected from the specified file descriptor. Auto-detection can be overruled by calling the function with explicit family, type, or proto arguments. This only affects how Python represents e.g. the return value of socket.getpeername() but not the actual OS resource. Unlike socket.fromfd(), fileno will return the same socket and not a duplicate. This may help close a detached socket using socket.close().

The newly created socket is non-inheritable.

socket对象不可继承。

 

 

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

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.

 

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.fromfd(fd, family, type, proto=0)

Duplicate the file descriptor fd (an integer as returned by a file object’s fileno() method) and build a socket object from the result. Address family, socket type and protocol number are as for the socket() function above. The file descriptor should refer to a socket, but this is not checked — subsequent operations on the object may fail if the file descriptor is invalid. This function is rarely needed, but can be used to get or set socket options on a socket passed to a program as standard input or output (such as a server started by the Unix inet daemon). The socket is assumed to be in blocking mode.

 

The newly created socket is non-inheritable.

 

1.2.2.   other functions

一些功能函数

socket.close(fd)

Close a socket file descriptor. This is like os.close(), but for sockets. On some platforms (most noticeable Windows) os.close() does not work for socket file descriptors.

socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)

socket.gethostbyname(hostname)

 

1.3.    socket objects

对象方法,基本含义都可以看名猜意,具体见相关文档。

函数

描述

服务器端套接字

s.bind()

绑定地址(host,port)到套接字, 在AF_INET下,以元组(host,port)的形式表示地址。

s.listen()

开始TCP监听。backlog指定在拒绝连接之前,操作系统可以挂起的最大连接数量。该值至少为1,大部分应用程序设为5就可以了。

s.accept()

被动接受TCP客户端连接,(阻塞式)等待连接的到来

客户端套接字

s.connect()

主动初始化TCP服务器连接,。一般address的格式为元组(hostname,port),如果连接出错,返回socket.error错误。

s.connect_ex()

connect()函数的扩展版本,出错时返回出错码,而不是抛出异常

公共用途的套接字函数

s.recv()

接收TCP数据,数据以字符串形式返回,bufsize指定要接收的最大数据量。flag提供有关消息的其他信息,通常可以忽略。

s.send()

发送TCP数据,将string中的数据发送到连接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。

s.sendall()

完整发送TCP数据,完整发送TCP数据。将string中的数据发送到连接的套接字,但在返回之前会尝试发送所有数据。成功返回None,失败则抛出异常。

s.recvfrom()

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

s.sendto()

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

s.close()

关闭套接字

s.getpeername()

返回连接套接字的远程地址。返回值通常是元组(ipaddr,port)。

s.getsockname()

返回套接字自己的地址。通常是一个元组(ipaddr,port)

s.setsockopt(level,optname,value)

设置给定套接字选项的值。

s.getsockopt(level,optname[.buflen])

返回套接字选项的值。

s.settimeout(timeout)

设置套接字操作的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。一般,超时期应该在刚创建套接字时设置,因为它们可能用于连接的操作(如connect())

s.gettimeout()

返回当前超时期的值,单位是秒,如果没有设置超时期,则返回None。

s.fileno()

返回套接字的文件描述符。

s.setblocking(flag)

如果flag为0,则将套接字设为非阻塞模式,否则将套接字设为阻塞模式(默认值)。非阻塞模式下,如果调用recv()没有发现任何数据,或send()调用无法立即发送数据,那么将引起socket.error异常。

s.makefile()

创建一个与该套接字相关连的文件

 

1.4.    Notes on socket timeouts

A socket object can be in one of three modes: blocking, non-blocking, or timeout. Sockets are by default always created in blocking mode, but this can be changed by calling setdefaulttimeout().

In blocking mode, operations block until complete or the system returns an error (such as connection timed out).

 

In non-blocking mode, operations fail (with an error that is unfortunately system-dependent) if they cannot be completed immediately: functions from the select can be used to know when and whether a socket is available for reading or writing.

In timeout mode, operations fail if they cannot be completed within the timeout specified for the socket (they raise a timeout exception) or if the system returns an error.

 

1.5.    案例1

使用socket传输文件。


# server
#
使用socket传输文件
import select
import socket
import queue
from time import sleep


def socket_service():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # get hostname
   
host = socket.gethostname()
    # set port
   
port = 9009
    try:
        s.bind((host, port))
        s.listen(3)
        c, addr = s.accept()
    except socket.error as msg:
        print(msg)
        exit(1)

    print(‘server ready, waiting for connetion...‘)

    fi = open(‘3.jpg‘,‘wb‘)
    while True:
        print(‘connetion addr:‘, addr)
        #c.send(‘first socket message.‘.encode())
       
data = c.recv(1024)
        if not data:
            break
       
fi.write(data)

    fi.close()
    c.close()
    s.close()

if __name__ == ‘__main__‘:
    #pr_type(‘s‘)
   
socket_service()

 


# client
#
使用socket传输文件
import socket

def socket_client():
    s = socket.socket()
    host = socket.gethostname()
    port = 9009

    s.connect((host, port))
    f = open(‘2.jpg‘, ‘rb‘)
    while 1:
        data = f.read(1024)
        if not data:
            break
       
s.send(data)
    f.close()
    s.close()


if __name__ == ‘__main__‘:
    #pr_type(‘s‘)
   
socket_client()

 

1.6.    案例2

socket一对多

测试方法:

相差2秒,依次运行三个client脚本

 

测试结果描述:

比较长,简单描述;

三个脚本均会每间隔1秒发送一条消息至sever,

console中看到第一个脚本的所有消息是间隔1秒显示;

第二个脚本的所有消息是瞬间打印

第三个脚本前面部分消息是瞬间打印,后面部分消息是间隔打印。

 

测试结果解析:

  1. 一对多不可以同时实现,只能是一对一结束后,另一个client连接;
  2. 根据结果推理,socket底层会接收所有信息,但的ython层级只会同时处理一个socket,待一个socket断开连接后再处理下一个。

 

#coding:utf-8

# server
#
使用socket传输文件
import select
import socket
import queue
from time import sleep


def socket_service():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # get hostname
   
host = socket.gethostname()
    # set port
   
port = 9009

    try:
        s.bind((host, port))
        s.listen(3)
        c, addr = s.accept()
        print(‘www‘)
    except socket.error as msg:
        print(msg)
        exit(1)
    else:
        print(‘server ready, waiting for connetion...‘)

    print(‘wwww‘)
    print(c)
    while True:
        print(‘connetion addr:‘, addr)
        #c.send(‘first socket message.‘.encode())
       
data = c.recv(1024)
        print(‘data reciveing: ‘, data.decode())
        if not data:
            #break
           
print(‘no data‘)
            c, addr = s.accept()
            print(c)

    c.close()
    s.close()

if __name__ == ‘__main__‘:
    #pr_type(‘s‘)
   
socket_service()

 

 

 

# client
#
使用socket传输文件
import socket
import time

def socket_client():
    s = socket.socket()
    host = socket.gethostname()
    port = 9009

    s.connect((host, port))
    print(s.getpeername())
    f = open(‘2.txt‘, ‘r‘, encoding=‘utf-8‘)
    while 1:
        data = f.readline()
        if not data:
            break
       
s.send(data.encode())
        time.sleep(1)

    f.close()
    s.close()


if __name__ == ‘__main__‘:
    #pr_type(‘s‘)
   
socket_client()

 

socket

标签:相关   for   return   encode   ipv4   lib   unix   NPU   exp   

原文地址:https://www.cnblogs.com/wodeboke-y/p/11241472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!