Python select模块使用实例(select,poll两种方式实现sever端)

注意:select 可以使用在任意平台上(windows & Linux),但是poll 只能使用在Linux下

 

select 实现:

# coding=utf-8
# @Author    : LYG
# @Time      : 2019/2/28 11:12
# @Name      : server.py
# coding: utf-8

import select
import socket


server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('localhost',1234))
server.listen(1)

inputs = [server,]
outputs = []
errors = []

print("服务器的socket:",server.fileno())

while True:
    read_list, write_list, err_list = select.select(inputs, outputs, errors)
    for item in read_list:
        if item is server:
            client, address = item.accept()
            print("嘿,有人跟你建立连接了:",client.fileno())
            inputs.append(client)
        else:
            msg = item.recv(1024)
            if msg != b'':
                print("收到{:s}消息:{:s}".format(str(item.getsockname()),msg.decode()))

                if item not in outputs:
                    outputs.append(item)
            else:
                print("不好,{:d}走了".format(item.fileno()))
                if item in outputs:
                    outputs.remove(item)
                inputs.remove(item)
                item.close()


    for item in outputs:
        item.send(("嗨喽!{:d}你好,我已经收到你的消息了".format(item.fileno())).encode())
        print("消息已经发出!")
        outputs.remove(item)


    for item in errors:
        print("完蛋,{:d} 出现异常了!".format(item.fileno()))
        inputs.remove(item)
        if item in outputs:
            outputs.remove(item)


 

POLL 实现:

# coding=utf-8
# @Author    : LYG
# @Time      : 2019/2/28 11:33
# @Name      : server1.py


import select
import socket


server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('localhost',5678))
server.listen(1)

poll = select.poll()
poll.register(server.fileno(), select.POLLIN)

print("服务器的socket:",server.fileno())

outputs = {}

while True:
    for fd, event in poll.poll():
        if event is select.POLLIN:
            if fd == server.fileno():
                client, address = server.accept()
                print("嘿,有人跟你建立连接了:", str(client.getsockname()))
                poll.register(client.fileno(), select.POLLIN)
                outputs[client.fileno()] = client
            else:
                socket = outputs[fd]
                msg = socket.recv(1024)
                if msg != b'':
                    print("收到{:s}消息:{:s}".format(str(socket.getsockname()), msg.decode()))
                    poll.modify(fd, select.POLLOUT)
                else:
                    print("不好,{:s}走了".format(str(socket.getsockname())))
                    socket.close()

        elif event is select.POLLOUT:
            socket = outputs[fd]
            socket.send(("嗨喽!{:s}你好,我已经收到你的消息了".format(str(socket.getsockname()))).encode())
            print("消息已经发出!")
            poll.modify(fd, select.POLLOUT)




 

 

client 实现:

# coding=utf-8
# @Author    : LYG
# @Time      : 2019/2/28 11:12
# @Name      : client.py
import socket

while True:
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 1234)

    print("我得去和{:s}建立连接".format(str(server_address)))

    client.connect(server_address)

    msg = input()
    client.send(msg.encode())

    data = client.recv(1024)
    print("收到服务端来的消息了:{:s}".format(data.decode()))

    print("结束了,我要关闭我自己~~")
    client.close()


 

你可能感兴趣的:(python,学习)