本节内容

Socket介绍
Socket参数介绍
基本Socket实例
Socket实现多连接处理
通过Socket实现简单SSH
通过Socket实现文件传送
作业开发一个支持多用户在线的FTP程序



 

1. Socket介绍

概念
A network socket is an endpoint of a connection across a computer network. Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets. More precisely, a socket is a handle (abstract reference) that a local program can pass to the networking application programming interface (API) to use the connection, for example "send this data on this socket".

For example, to send "Hello, world!" via TCP to port 80 of the host with address 1.2.3.4, one might get a socket, connect it to the remote host, send the string, then close the socket.

实现一个socket至少要分以下几步(伪代码)

Socket socket = getSocket(type = "TCP")  #设定好协议类型
connect(socket, address = "1.2.3.4", port = "80") #连接远程机器
send(socket, "Hello, world!") #发送消息
close(socket) #关闭连接


A socket API is an application programming interface (API), usually provided by the operating system, that allows application programs to control and use network sockets. Internet socket APIs are usually based on the Berkeley sockets standard. In the Berkeley sockets standard, sockets are a form of file descriptor (a file handle), due to the Unix philosophy that "everything is a file", and the analogies between sockets and files: you can read, write, open, and close both.   

A socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Sockets need not have an address (for example for only sending data), but if a program binds a socket to an address, the socket can be used to receive data sent to that address. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or thread.

Socket Families(地址簇)
socket.AF_UNIX unix本机进程间通信

socket.AF_INET IPV4 

socket.AF_INET6  IPV6

These constants represent the address (and protocol) families, used for the first argument to socket(). If the AF_UNIX constant is not defined then this protocol is unsupported. More constants may be available depending on the system.

Socket Types
socket.SOCK_STREAM  #for tcp

socket.SOCK_DGRAM   #for udp

socket.SOCK_RAW     #原始套接字普通的套接字无法处理ICMP、IGMP等网络报文而SOCK_RAW可以其次SOCK_RAW也可以处理特殊的IPv4报文此外利用原始套接字可以通过IP_HDRINCL套接字选项由用户构造IP头。

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

socket.SOCK_SEQPACKET #废弃了

These constants represent the socket types, used for the second argument to socket(). More constants may be available depending on the system. (Only SOCK_STREAM and SOCK_DGRAM appear to be generally useful.)

 

2. Socket 参数介绍

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 or AF_RDS. The socket type should beSOCK_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 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().

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.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0) #获取要连接的对端主机地址 必会

sk.bind(address) 必会

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

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是形式为ipaddrport的元组指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。

sk.settimeout(timeout) 必会

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

sk.getpeername()  必会

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

sk.getsockname()

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

sk.fileno()

  套接字的文件描述符

socket.sendfile(file, offset=0, count=None)

     发送文件 但目前多数情况下并无什么卵用。

 

3. 基本Socket实例

前面讲了这么多到底咋么用呢

SocketServer.py

import socket

server = socket.socket() #获得socket实例
 
server.bind(("localhost",9998)) #绑定ip port
server.listen()  #开始监听
print("等待客户端的连接...")
conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...
print("新连接:",addr )

data = conn.recv(1024)
print("收到消息:",data)

server.close()


SocketClient.py

import socket

client = socket.socket()

client.connect(("localhost",9998))

client.send(b"hey")

client.close()

上面的代码的有一个问题 就是SocketServer.py运行起来后 接收了一次客户端的data就退出了。。。 但实际场景中一个连接建立起来后可能要进行多次往返的通信。

wKiom1fazDWQvyeRAAdNlr2wxj0312.png

多次的数据交互怎么实现呢

socketserver 支持多次交互

import socket
  
 server = socket.socket() #获得socket实例
  
server.bind(("localhost",9998)) #绑定ip port
server.listen()  #开始监听
print("等待客户端的连接...")
conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...
print("新连接:",addr )
while True:

    data = conn.recv(1024)

    print("收到消息:",data)
    conn.send(data.upper())

server.close()

socket客户端支持多交互

import socket

client = socket.socket()

client.connect(("localhost",9998))

while True:
    msg = input(">>:").strip()
    if len(msg) == 0:continue
    client.send( msg.encode("utf-8") )

    data = client.recv(1024)
    print("来自服务器:",data)

client.close()

实现了多次交互 棒棒的 但你会发现一个小问题 就是客户端一断开服务器端就进入了死循环为啥呢

看客户端断开时服务器端的输出

等待客户端的连接...
新连接: ('127.0.0.1', 62722)
收到消息: b'hey'
收到消息: b'you'
收到消息: b''  #客<span style="color: #ff0000;">户端一断开服务器端就收不到数据了但是不会报错就进入了死循环模式。。</span>。
收到消息: b''
收到消息: b''
收到消息: b''
收到消息: b''

知道了原因就好解决了只需要加个判断服务器接到的数据是否为空就好了为空就代表断了。。。

加了判断客户端是否断开的代码

import socket

server = socket.socket() #获得socket实例

server.bind(("localhost",9998)) #绑定ip port
server.listen()  #开始监听
print("等待客户端的连接...")
conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...
print("新连接:",addr )
while True:

    data = conn.recv(1024)
    if not data:
        print("客户端断开了...")
        break
    print("收到消息:",data)
    conn.send(data.upper())

server.close()



4.Socket实现多连接处理

上面的代码虽然实现了服务端与客户端的多次交互但是你会发现如果客户端断开了 服务器端也会跟着立刻断开因为服务器只有一个while 循环客户端一断开服务端收不到数据 就会直接break跳出循环然后程序就退出了这显然不是我们想要的结果 我们想要的是客户端如果断开了我们这个服务端还可以为下一个客户端服务它不能断她接完一个客擦完嘴角的遗留物就要接下来勇敢的去接待下一个客人。 在这里如何实现呢

conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...

我们知道上面这句话负责等待并接收新连接对于上面那个程序其实在while break之后只要让程序再次回到上面这句代码这就可以让服务端继续接下一个客户啦。

import socket
 
server = socket.socket() #获得socket实例
 
server.bind(("localhost",9998)) #绑定ip port
server.listen()  #开始监听
 
while True: #第一层loop
    print("等待客户端的连接...")
    conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...
    print("新连接:",addr )
    while True:
 
        data = conn.recv(1024)
        if not data:
            print("客户端断开了...")
            break #这里断开就会再次回到第一次外层的loop
        print("收到消息:",data)
        conn.send(data.upper())
 
server.close()

注意了 此时服务器端依然只能同时为一个客户服务其客户来了得排队(连接挂起)不能玩 three some. 这时你说想我就想玩3p,就想就想嘛其实也可以多交钱嘛继续往下看后面开启新姿势后就可以玩啦。。。

 

5.通过socket实现简单的ssh

光只是简单的发消息、收消息没意思干点正事可以做一个极简版的ssh就是客户端连接上服务器后让服务器执行命令并返回结果给客户端。

socket ssh server

import socket
import os

server = socket.socket() #获得socket实例
#server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

server.bind(("localhost",9998)) #绑定ip port
server.listen()  #开始监听

while True: #第一层loop
    print("等待客户端的连接...")
    conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...
    print("新连接:",addr )
    while True:

        data = conn.recv(1024)
        if not data:
            print("客户端断开了...")
            break #这里断开就会再次回到第一次外层的loop
        print("收到命令:",data)
        res = os.popen(data.decode()).read() #py3 里socket发送的只有bytes,os.popen又只能接受str,所以要decode一下
        print(len(res))
        conn.send(res.encode("utf-8"))

server.close()


socket ssh client

import socket

client = socket.socket()

client.connect(("localhost",9998))

while True:
    msg = input(">>:").strip()
    if len(msg) == 0:continue
    client.send( msg.encode("utf-8") )

    data = client.recv(1024)
    print(data.decode()) #命令执行结果

client.close()

very cool , 这样我们就做了一个简单的ssh , 但多试几条命令你就会发现上面的程序有以下2个问题。

不能执行top等类似的 会持续输出的命令,这是因为服务器端在收到客户端指令后会一次性通过os.popen执行并得到结果后返回给客户但top这样的命令用os.popen执行你会发现永远都不会结束所以客户端也永远拿不到返回。(真正的ssh是通过select 异步等模块实现的我们以后会涉及)
不能执行像cd这种没有返回的指令 因为客户端每发送一条指令就会通过client.recv(1024)等待接收服务器端的返回结果但是cd命令没有结果 服务器端调用conn.send(data)时是不会发送数据给客户端的。 所以客户端就会一直等着等到天荒地老结果就卡死了。解决的办法是在服务器端判断命令的执行返回结果的长度如果结果为空就自己加个结果返回给客户端如写上"cmd exec success, has no output."
如果执行的命令返回结果的数据量比较大会发现结果返回不全在客户端上再执行一条命令结果返回的还是上一条命令的后半段的执行结果这是为什么呢这是因为我们的客户写client.recv(1024) 即客户端一次最多只接收1024个字节如果服务器端返回的数据是2000字节那有至少9百多字节是客户端第一次接收不了的那怎么办呢服务器端此时不能把数据直接扔了呀so它会暂时存在服务器的io发送缓冲区里等客户端下次再接收数据的时候再发送给客户端。 这就是为什么客户端执行第2条命令时却接收到了第一条命令的结果的原因。 这时有同学说了 那我直接在客户端把client.recv(1024)改大一点不就好了么 改成一次接收个100mb,哈哈这是不行的因为socket每次接收和发送都有最大数据量限制的毕竟网络带宽也是有限的呀不能一次发太多发送的数据最大量的限制 就是缓冲区能缓存的数据的最大量这个缓冲区的最大值在不同的系统上是不一样的 我实在查不到一个具体的数字但测试的结果是在linux上最大一次可接收10mb左右的数据不过官方的建议是不超过8k,也就是8192并且数据要可以被2整除不要问为什么 。anyway , 如果一次只能接收最多不超过8192的数据 那服务端返回的数据超过了这个数字怎么办呢比如让服务器端打开一个5mb的文件并返回客户端怎么才能完整的接受到呢那就只能循环收取啦。
 

在开始解决上面问题3之前我们要考虑客户端要循环接收服务器端的大量数据返回直到一条命令的结果全部返回为止 但问题是客户端知道服务器端返回的数据有多大么答案是不知道那既然不知道服务器的要返回多大的数据那客户端怎么知道要循环接收多少次呢答案是不知道擦那咋办 总不能靠猜吧呵呵。。。 当然不能那只能让服务器在发送数据之前主动告诉客户端要发送多少数据给客户端然后再开始发送数据yes, 机智如我搞起。

先简单测试接收数据量大小
ssh server 返回执行结果大小

import socket
import os,subprocess

server = socket.socket() #获得socket实例
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

server.bind(("localhost",9998)) #绑定ip port
server.listen()  #开始监听

while True: #第一层loop
    print("等待客户端的连接...")
    conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...
    print("新连接:",addr )
    while True:

        data = conn.recv(1024)
        if not data:
            print("客户端断开了...")
            break #这里断开就会再次回到第一次外层的loop
        print("收到命令:",data)
        #res = os.popen(data.decode()).read() #py3 里socket发送的只有bytes,os.popen又只能接受str,所以要decode一下
        res = subprocess.Popen(data,shell=True,stdout=subprocess.PIPE).stdout.read() #跟上面那条命令的效果是一样的
        if len(res) == 0:
            res = "cmd exec success,has not output!"
        conn.send(str(len(res)).endcode("utf-8")) #发送数据之前,先告诉客户端要发多少数据给它
        conn.sendall(res.encode("utf-8")) #发送端也有最大数据量限制,所以这里用sendall,相当于重复循环调用conn.send,直至数据发送完毕

server.close()


ssh client 接收执行结果的大小

import socket

client = socket.socket()

client.connect(("localhost",9998))

while True:
    msg = input(">>:").strip()
    if len(msg) == 0:continue
    client.send( msg.encode("utf-8") )

    res_return_size  = client.recv(1024) #接收这条命令执行结果的大小
    print("getting cmd result , ", res_return_size)
    total_rece_size = int(res_return_size)
    print(total_rece_size)

    #print(data.decode()) #命令执行结果

client.close()
结果输出:<br>/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/jieli/PycharmProjects/python基础/自动化day8socket/sock_client.py
>>:cat /var/log/system.log
getting cmd result ,  b'3472816Sep  9 09:06:37 Jies-MacBook-Air kernel[0]: hibernate p_w_picpath path: /var/vm/sleepp_w_picpath\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: efi pagecount 65\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: hibernate_page_list_setall(preflight 1) start\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: hibernate_page_list_setall time: 211 ms\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: pages 1211271, wire 225934, act 399265, inact 4, cleaned 0 spec 97, zf 3925, throt 0, compr 218191, xpmapped 40000\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: could discard act 94063 inact 129292 purgeable 58712 spec 81788 cleaned 0\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: WARNING: hibernate_page_list_setall skipped 47782 xpmapped pages\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: hibernate_page_list_setall preflight pageCount 225934 est comp 41 setfile 421527552 min 1073741824\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: kern_open_file_for_direct_io(0)\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: kern_open_file_for_direct_io took 181 ms\nSep  9 '
Traceback (most recent call last):
  File "/Users/jieli/PycharmProjects/python基础/自动化day8socket/sock_client.py", line 17, in <module>
    total_rece_size = int(res_return_size)
ValueError: invalid literal for int() with base 10: b'3472816Sep  9 09:06:37 Jies-MacBook-Air kernel[0]: hibernate p_w_picpath path: /var/vm/sleepp_w_picpath\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]: efi pagecount 65\nSep  9 09:06:37 Jies-MacBook-Air kernel[0]:
 
Process finished with exit code 1

看程序执行报错了 我在客户端本想只接服务器端命令的执行结果但实际上却连命令结果也跟着接收了一部分。 这是为什么呢服务器不是只send了结果的大小么不应该只是个数字么尼玛命令结果不是第2次send的时候才发送的么擦擦擦价值观都要崩溃了啊。。。。

哈哈这里就引入了一个重要的概念“粘包” 即服务器端你调用时send 2次但你send调用时数据其实并没有立刻被发送给客户端而是放到了系统的socket发送缓冲区里等缓冲区满了、或者数据等待超时了数据才会被send到客户端这样就把好几次的小数据拼成一个大数据统一发送到客户端了这么做的目地是为了提高io利用效率一次性发送总比连发好几次效率高嘛。 但也带来一个问题就是“粘包”即2次或多次的数据粘在了一起统一发送了。就是我们上面看到的情况 。

我们在这里必须要想办法把粘包分开 因为不分开你就没办法取出来服务器端返回的命令执行结果的大小呀。so ,那怎么分开呢首先你是没办法让缓冲区强制刷新把数据发给客户端的。 你能做的只有一个。就是让缓冲区超时超时了系统就不会等缓冲区满了会直接把数据发走因为不能一个劲的等后面的数据呀等太久会造成数据延迟了那可是极不好的。so如果让缓冲区超时呢

答案就是

time.sleep(0.5),经多次测试让服务器程序sleep 至少0.5就会造成缓冲区超时。哈哈哈 你会说擦这么玩不会被老板开除么虽然我们觉得0.5s不多但是对数据实时要求高的业务场景比如股票交易过了0.5s 股票价格可以就涨跌很多搞毛线呀。但没办法我刚学socket的时候 找不到更好的办法就是这么玩的现在想想也真是low呀
但现在我是有Tesla的男人了不能再这么low了 所以推出nb新姿势就是 不用sleep,服务器端每发送一个数据给客户端就立刻等待客户端进行回应即调用 conn.recv(1024), 由于recv在接收不到数据时是阻塞的这样就会造成服务器端接收不到客户端的响应就不会执行后面的conn.sendall(命令结果)的指令收到客户端响应后再发送命令结果时缓冲区就已经被清空了因为上一次的数据已经被强制发到客户端了。 好机智 看下面代码实现。


接收大数据 server端

#_*_coding:utf-8_*_
__author__ = 'Alex Li'

import socket
import os,subprocess

server = socket.socket() #获得socket实例
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

server.bind(("localhost",9999)) #绑定ip port
server.listen()  #开始监听

while True: #第一层loop
    print("等待客户端的连接...")
    conn,addr = server.accept() #接受并建立与客户端的连接,程序在此处开始阻塞,只到有客户端连接进来...
    print("新连接:",addr )
    while True:

        data = conn.recv(1024)
        if not data:
            print("客户端断开了...")
            break #这里断开就会再次回到第一次外层的loop
        print("收到命令:",data)
        #res = os.popen(data.decode()).read() #py3 里socket发送的只有bytes,os.popen又只能接受str,所以要decode一下
        res = subprocess.Popen(data,shell=True,stdout=subprocess.PIPE).stdout.read() #跟上面那条命令的效果是一样的
        if len(res) == 0:
            res = "cmd exec success,has not output!".encode("utf-8")
        conn.send(str(len(res)).encode("utf-8")) #发送数据之前,先告诉客户端要发多少数据给它
        print("等待客户ack应答...")
        client_final_ack = conn.recv(1024) #等待客户端响应
        print("客户应答:",client_final_ack.decode())
        print(type(res))
        conn.sendall(res) #发送端也有最大数据量限制,所以这里用sendall,相当于重复循环调用conn.send,直至数据发送完毕

server.close()


接收大数据客户端

#_*_coding:utf-8_*_
__author__ = 'Alex Li'

import socket
import sys

client = socket.socket()

client.connect(("localhost",9999))

while True:
    msg = input(">>:").strip()
    if len(msg) == 0:continue
    client.send( msg.encode("utf-8") )

    res_return_size  = client.recv(1024) #接收这条命令执行结果的大小
    print("getting cmd result , ", res_return_size)
    total_rece_size = int(res_return_size)
    print("total size:",res_return_size)
    client.send("准备好接收了,发吧loser".encode("utf-8"))
    received_size = 0 #已接收到的数据
    cmd_res = b''
    f = open("test_copy.html","wb")#把接收到的结果存下来,一会看看收到的数据 对不对
    while received_size != total_rece_size: #代表还没收完
        data = client.recv(1024)
        received_size += len(data) #为什么不是直接1024,还判断len干嘛,注意,实际收到的data有可能比1024少
        cmd_res += data
    else:
        print("数据收完了",received_size)
        #print(cmd_res.decode())
        f.write(cmd_res) #把接收到的结果存下来,一会看看收到的数据 对不对
    #print(data.decode()) #命令执行结果

client.close()


6. SocketServer

The socketserver module simplifies the task of writing network servers.

socketserver一共有这么几种类型

class socketserver.TCPServer(server_address, RequestHandlerClass, bind_and_activate=True)


This uses the Internet TCP protocol, which provides for continuous streams of data between the client and server.

class socketserver.UDPServer(server_address, RequestHandlerClass, bind_and_activate=True)

This uses datagrams, which are discrete packets of information that may arrive out of order or be lost while in transit. The parameters are the same as for TCPServer.

class socketserver.UnixStreamServer(server_address, RequestHandlerClass, bind_and_activate=True)
class socketserver.UnixDatagramServer(server_address, RequestHandlerClass,bind_and_activate=True)

    These more infrequently used classes are similar to the TCP and UDP classes, but use Unix domain sockets; they’re not available on non-Unix platforms. The parameters are the same as for TCPServer.

There are five classes in an inheritance diagram, four of which represent synchronous servers of four types:

+------------+
| BaseServer |
+------------+
      |
      v
+-----------+        +------------------+
| TCPServer |------->| UnixStreamServer |
+-----------+        +------------------+
      |
      v
+-----------+        +--------------------+
| UDPServer |------->| UnixDatagramServer |
+-----------+        +--------------------+

创建一个socketserver 至少分以下几步:

First, you must create a request handler class by subclassing the BaseRequestHandlerclass and overriding its handle() method; this method will process incoming requests.   
Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class.
Then call the handle_request() orserve_forever() method of the server object to process one or many requests.
Finally, call server_close() to close the socket.
基本的socketserver代码

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    """
    The request handler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Create the server, binding to localhost on port 9999
    server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()


但你发现上面的代码依然不能同时处理多个连接擦那我搞这个干嘛别急不是不能处理多并发如果你想你还要启用多线程多线程我们现在还没讲但你大体知道有了多线程就能同时让cpu干多件事了就行先。

 

让你的socketserver并发起来 必须选择使用以下一个多并发的类

class socketserver.ForkingTCPServer

class socketserver.ForkingUDPServer

class socketserver.ThreadingTCPServer

class socketserver.ThreadingUDPServer

 

so 只需要把下面这句

server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)


换成下面这个就可以多并发了这样客户端每连进一个来服务器端就会分配一个新的线程来处理这个客户端的请求

   server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler)


class socketserver.BaseServer(server_address, RequestHandlerClass) 主要有以下方法

lass socketserver.BaseServer(server_address, RequestHandlerClass)
This is the superclass of all Server objects in the module. It defines the interface, given below, but does not implement most of the methods, which is done in subclasses. The two parameters are stored in the respective server_address and RequestHandlerClass attributes.

fileno()
Return an integer file descriptor for the socket on which the server is listening. This function is most commonly passed to selectors, to allow monitoring multiple servers in the same process.

handle_request()
Process a single request. This function calls the following methods in order: get_request(), verify_request(), and process_request(). If the user-provided handle() method of the handler class raises an exception, the server’s handle_error() method will be called. If no request is received within timeout seconds, handle_timeout() will be called and handle_request() will return.

serve_forever(poll_interval=0.5)
Handle requests until an explicit shutdown() request. Poll for shutdown every poll_interval seconds. Ignores the timeout attribute. It also calls service_actions(), which may be used by a subclass or mixin to provide actions specific to a given service. For example, the ForkingMixIn class uses service_actions() to clean up zombie child processes.

Changed in version 3.3: Added service_actions call to the serve_forever method.

service_actions()
This is called in the serve_forever() loop. This method can be overridden by subclasses or mixin classes to perform actions specific to a given service, such as cleanup actions.

New in version 3.3.

shutdown()
Tell the serve_forever() loop to stop and wait until it does.

server_close()
Clean up the server. May be overridden.

address_family
The family of protocols to which the server’s socket belongs. Common examples are socket.AF_INET and socket.AF_UNIX.

RequestHandlerClass
The user-provided request handler class; an instance of this class is created for each request.

server_address
The address on which the server is listening. The format of addresses varies depending on the protocol family; see the documentation for the socket module for details. For Internet protocols, this is a tuple containing a string giving the address, and an integer port number: ('127.0.0.1', 80), for example.

socket
The socket object on which the server will listen for incoming requests.

The server classes support the following class variables:

allow_reuse_address
Whether the server will allow the reuse of an address. This defaults to False, and can be set in subclasses to change the policy.

request_queue_size
The size of the request queue. If it takes a long time to process a single request, any requests that arrive while the server is busy are placed into a queue, up to request_queue_size requests. Once the queue is full, further requests from clients will get a “Connection denied” error. The default value is usually 5, but this can be overridden by subclasses.

socket_type
The type of socket used by the server; socket.SOCK_STREAM and socket.SOCK_DGRAM are two common values.

timeout
Timeout duration, measured in seconds, or None if no timeout is desired. If handle_request() receives no incoming requests within the timeout period, the handle_timeout() method is called.

There are various server methods that can be overridden by subclasses of base server classes like TCPServer; these methods aren’t useful to external users of the server object.

finish_request()
Actually processes the request by instantiating RequestHandlerClass and calling its handle() method.

get_request()
Must accept a request from the socket, and return a 2-tuple containing the new socket object to be used to communicate with the client, and the client’s address.

handle_error(request, client_address)
This function is called if the handle() method of a RequestHandlerClass instance raises an exception. The default action is to print the traceback to standard output and continue handling further requests.

handle_timeout()
This function is called when the timeout attribute has been set to a value other than None and the timeout period has passed with no requests being received. The default action for forking servers is to collect the status of any child processes that have exited, while in threading servers this method does nothing.

process_request(request, client_address)
Calls finish_request() to create an instance of the RequestHandlerClass. If desired, this function can create a new process or thread to handle the request; the ForkingMixIn and ThreadingMixIn classes do this.

server_activate()
Called by the server’s constructor to activate the server. The default behavior for a TCP server just invokes listen() on the server’s socket. May be overridden.

server_bind()
Called by the server’s constructor to bind the socket to the desired address. May be overridden.

verify_request(request, client_address)
Must return a Boolean value; if the value is True, the request will be processed, and if it’s False, the request will be denied. This function can be overridden to implement access controls for a server. The default implementation always returns True.