Exemplo n.º 1
0
#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandom-rhodes/fopnp/blob/mn/py3/chapter07/srv_threaded.py

import zen_utils
from threading import Thread


def start_threads(listener, workers=4):
    t = (listener, )
    for i in range(workers):
        Thread(target=zen_utils.accept_connections_forever, args=t).start()


if __name__ == '__main__':
    address = zen_utils.parse_command_line('multi-threaded server')
    listener = zen_utils.create_srv_socket(address)
    start_threads(listener)
Exemplo n.º 2
0
#! /usr/bin/env python3
# coding = utf-8

# srv_single.py - 单线程服务器

import zen_utils

if __name__ == '__main__':
    address = zen_utils.parse_command_line('simple single-threaded server')
    listener = zen_utils.create_srv_socket(address)
    zen_utils.accept_connect_forever(listener)
Exemplo n.º 3
0
def main():
    address = zen_utils.parse_command_line("Multi-threaded server")
    listener = zen_utils.create_srv_socket(address)
    start_threads(listener)
Exemplo n.º 4
0
            addresses[sock] = address  # 把套接字名在字典做映射
            poll_object.register(
                sock, select.POLLIN)  # 在 fd_set 数据结构中注册当前套接字并设定 poll in 状态
        # incoming data: keep receiving until we see the suffix
        elif event & select.POLLIN:
            more_data = sock.recv(4096)
            if not more_data:  # end of file
                sock.close()  # next poll() will POLLNVAL, and thus clean up
                continue
            data = bytes_received.pop(sock, b'') + more_data
            if data.endswith(b'?'):
                bytes_to_send[sock] = zen_utils.get_answer(data)
                poll_object.modify(sock, select.POLLOUT)
            else:
                bytes_received[sock] = data
        # socket ready to send: keep sending until all bytes are delivered.
        elif event & select.POLLOUT:
            data = bytes_to_send.pop(sock)
            n = sock.send(data)
            if n < len(data):
                bytes_to_send[sock] = data[n:]
            else:
                poll_object.modify(sock, select.POLLIN)


if __name__ == '__main__':
    address = zen_utils.parse_command_line(
        "low-level async server")  # 返回一个 tuple 地址
    listener = zen_utils.create_srv_socket(address)  # 返回一个最大64个的监听套接字
    server(listener)
Exemplo n.º 5
0
def main():
    address = zen_utils.parse_command_line("Multi-threaded server")
    listener = zen_utils.create_srv_socket(address)
    start_threads(listener)
Exemplo n.º 6
0
                sockets[sock.fileno()] = sock  #将连接套接字添加进sockets字典中
                addresses[sock] = address  #将连接套接字的远端地址添加进address字典中
                poll_object.register(
                    sock, select.POLLIN)  #向poll对象注册sock连接套接字,以监视它的【POLLIN事件】

            elif event & select.POLLIN:
                more_data = sock.recv(4096)
                if not more_data:
                    sock.close()
                    continue
                data = bytes_received.pop(sock, b'') + more_data
                if data.endswith(b'?'):
                    bytes_to_send[sock] = zen_utils.get_answer(data)
                    poll_object.modify(sock, select.POLLOUT)
                else:
                    bytes_received[sock] = data

            elif event & select.POLLOUT:
                data = bytes_to_send.pop(sock)
                n = sock.send(data)
                if n < len(data):
                    bytes_to_send[sock] = data[n:]
                else:
                    poll_object.modify(sock, select.POLLIN)


if __name__ == "__main__":
    address = zen_utils.parse_command_line('low-level async server')
    listener = zen_utils.create_srv_socket(address)  #创建并返回一个绑定在指定addr上的监听套接字
    serve(listener)