EPOLL版

server

root@iZ947mgy3c5Z:/prodata/scripts# cat epoll_tcp_server.py 
import time
import select
from socket import *

def main():
    server_socket = socket(AF_INET, SOCK_STREAM)
    server_port = ('127.0.0.1', 7788)
    server_socket.bind(server_port)
    server_socket.listen(5)
    connection = {}
    addresses = {}
    epoll = select.epoll()
    epoll.register(int(server_socket.fileno()), select.EPOLLIN|select.EPOLLET)
    while True:
        try:
            epoll_list = epoll.poll()
        except Exception as e:
            # 执行中没有发现异常
            print(str(e))
            # pass
        else:  
            # print(epoll_list)
            for fd, events in epoll_list:
                if int(fd) == server_socket.fileno():
                    client_socket, client_addr = server_socket.accept()
                    connection[client_socket.fileno()] = client_socket
                    addresses[client_socket.fileno()] = client_addr
                    epoll.register(client_socket.fileno(), select.EPOLLIN|select.EPOLLET)
                else:
                    rs = connection[fd]
                    client_data = rs.recv(1024)
                    if len(client_data) <= 0:
                        socket_list.remove(rs)
                        print('{}: disconnect...'.format(addresses[fd]))
                    else:
                        print('[{}]: {}'.format(time.strftime("%Y-%m-%d %H:%M:%S"), client_data))
                        rs.send(client_data)

if __name__ == '__main__':
    main()

client

root@iZ947mgy3c5Z:/prodata/scripts# cat multi_thread_client.py 
from socket import *
from multiprocessing import Pool, current_process
from threading import Thread, current_thread
import time

host_port = ('127.0.0.1', 7788)
def worker():
    try:
        client_socket = socket(AF_INET, SOCK_STREAM)
        client_socket.connect(host_port)
        while True:
            time.sleep(1)
            client_socket.send('hi'.encode())

            print('[{}]: {}'.format(time.strftime("%Y-%m-%d %H:%M:%S"), current_thread().name))
    finally:
       client_socket.close()

def main():
    t1 = Thread(target=worker, name="t1")
    t2 = Thread(target=worker, name="t2")
    t3 = Thread(target=worker, name="t3")
    t4 = Thread(target=worker, name="t4")
    t1.start()
    t2.start()
    t3.start()
    t4.start()


if __name__ == '__main__':
    main()

效果

server

root@iZ947mgy3c5Z:/prodata/scripts# python3.5 epoll_tcp_server.py 
[2018-01-24 14:13:05]: b'hi'
[2018-01-24 14:13:05]: b'hi'
[2018-01-24 14:13:05]: b'hi'
[2018-01-24 14:13:05]: b'hi'
[2018-01-24 14:13:06]: b'hi'
[2018-01-24 14:13:06]: b'hi'
[2018-01-24 14:13:06]: b'hi'
[2018-01-24 14:13:06]: b'hi'
[2018-01-24 14:13:07]: b'hi'
[2018-01-24 14:13:07]: b'hi'
[2018-01-24 14:13:07]: b'hi'
[2018-01-24 14:13:07]: b'hi'
[2018-01-24 14:13:08]: b'hi'
[2018-01-24 14:13:08]: b'hi'
[2018-01-24 14:13:08]: b'hi'
[2018-01-24 14:13:08]: b'hi'
[2018-01-24 14:13:09]: b'hi'
[2018-01-24 14:13:09]: b'hi'
[2018-01-24 14:13:09]: b'hi'
[2018-01-24 14:13:09]: b'hi'
[2018-01-24 14:13:10]: b'hi'
[2018-01-24 14:13:10]: b'hi'
[2018-01-24 14:13:10]: b'hi'

client

root@iZ947mgy3c5Z:/prodata/scripts# python3.5 multi_thread_client.py 
[2018-01-24 14:13:05]: t1
[2018-01-24 14:13:05]: t2
[2018-01-24 14:13:05]: t3
[2018-01-24 14:13:05]: t4
[2018-01-24 14:13:06]: t2
[2018-01-24 14:13:06]: t1
[2018-01-24 14:13:06]: t3
[2018-01-24 14:13:06]: t4
[2018-01-24 14:13:07]: t2
[2018-01-24 14:13:07]: t1
[2018-01-24 14:13:07]: t3
[2018-01-24 14:13:07]: t4
[2018-01-24 14:13:08]: t1
[2018-01-24 14:13:08]: t2
[2018-01-24 14:13:08]: t3
[2018-01-24 14:13:08]: t4
[2018-01-24 14:13:09]: t2
[2018-01-24 14:13:09]: t1
[2018-01-24 14:13:09]: t3
[2018-01-24 14:13:09]: t4
[2018-01-24 14:13:10]: t1
[2018-01-24 14:13:10]: t2
[2018-01-24 14:13:10]: t3
[2018-01-24 14:13:10]: t4

results matching ""

    No results matching ""