示例#1
0
文件: test_io.py 项目: GMLudo/curio
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     await sock.send(b'Msg1\nMsg2\nMsg3\n')
     await sock.close()
     await serv.cancel()
示例#2
0
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     s = sock.as_stream()
     await s.writelines([b'Msg1\n', b'Msg2\n', b'Msg3\n'])
     await sock.close()
示例#3
0
 async def client(host, port, context):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect((host, port))
     ssl_sock = await context.wrap_socket(sock, server_hostname=host)
     await ssl_sock.sendall(b'Hello, world!')
     resp = await ssl_sock.recv(4096)
     return resp
示例#4
0
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     await sock.send(b'Message')
     data = await sock.recv(8192)
     await sock.close()
示例#5
0
文件: server.py 项目: Niksko/LightPet
async def main():
    # Set up a logger for this module
    logging.basicConfig(level=logging.INFO)

    logger = logging.getLogger(__name__)
    fh = logging.FileHandler(datetime.datetime.utcnow().isoformat() + 'lightpet.log')
    fh.addFilter(filter_data)
    logger.addHandler(fh)

    # First, load the config from the config file
    config = load_config_fromfile('common/configuration.json')

    # Break out the values from the config object into handy variables
    udp_port = config["UDP_PORT"]
    server_advertisement_message = config["SERVER_SERVICE_MESSAGE"]

    # Next, setup the socket according to these config parameters
    udpSocket = socket(AF_INET, SOCK_DGRAM)
    udpSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    udpSocket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
    udpSocket.bind(('', udp_port))

    # Spawn the server advertisements
    advertisement_task = await curio.spawn(advertise(udpSocket, server_advertisement_message, udp_port))

    # Receive messages and output them
    while True:
        data, _ = await udpSocket.recvfrom(512)
        # Spawn a data_handler task to handle this data
        await curio.spawn(data_handler(data))
示例#6
0
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     await sock.send(b'Msg1\nMsg2\nMsg3\n')
     await sock.close()
     await serv.cancel()
示例#7
0
async def main():
    parser = argparse.ArgumentParser(
        description='Run a Tic Tac Toe game server')
    parser.add_argument('board_x_size',
                        metavar='X',
                        type=int,
                        nargs='?',
                        default=3,
                        help='The horizontal size of the board')
    parser.add_argument('board_y_size',
                        metavar='Y',
                        type=int,
                        nargs='?',
                        default=3,
                        help='The vertical size of the board')
    parser.add_argument(
        'needed_symbols',
        metavar='Z',
        type=int,
        nargs='?',
        default=3,
        help=
        'The number of consecutive symbols that account for a winning move.')
    parser.add_argument('player_count',
                        metavar='P',
                        type=int,
                        nargs='?',
                        default=2,
                        help='The number of players.',
                        choices=range(2, 10))
    args = parser.parse_args()

    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    server_socket.bind((HOSTNAME, PORT))
    server_socket.listen(15)

    active_games = []
    curr_game_serv = GameServer(player_count=args.player_count,
                                board_x_size=args.board_x_size,
                                board_y_size=args.board_y_size,
                                needed_symbols=args.needed_symbols)

    while True:
        client_socket, address = await server_socket.accept()
        print(f'Accepted a player - {client_socket} @ {address}')
        await curr_game_serv.add_player(client_socket, address
                                        )  # add the player to the server
        print(
            f'Current game server is hosting {len(curr_game_serv.players)}/{len(curr_game_serv.players)} players'
        )
        if curr_game_serv.should_start():
            # Start the game and start accepting players for the next game
            print('Starting Game')
            await spawn(curr_game_serv.start_game)
            active_games.append(curr_game_serv)
            curr_game_serv = GameServer(player_count=args.player_count,
                                        board_x_size=args.board_x_size,
                                        board_y_size=args.board_y_size,
                                        needed_symbols=args.needed_symbols)
示例#8
0
 async def client(host, port, context):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect((host, port))
     ssl_sock = await context.wrap_socket(sock, server_hostname=host)
     await ssl_sock.sendall(b'Hello, world!')
     resp = await ssl_sock.recv(4096)
     return resp
示例#9
0
async def main():
    parser = argparse.ArgumentParser(description='Run a Tic Tac Toe game server')
    parser.add_argument('board_x_size', metavar='X', type=int, nargs='?', default=3,
                        help='The horizontal size of the board')
    parser.add_argument('board_y_size', metavar='Y', type=int, nargs='?', default=3,
                        help='The vertical size of the board')
    parser.add_argument('needed_symbols', metavar='Z', type=int, nargs='?', default=3,
                        help='The number of consecutive symbols that account for a winning move.')
    parser.add_argument('player_count', metavar='P', type=int, nargs='?', default=2,
                        help='The number of players.', choices=range(2, 10))
    args = parser.parse_args()

    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    server_socket.bind((HOSTNAME, PORT))
    server_socket.listen(15)

    active_games = []
    curr_game_serv = GameServer(player_count=args.player_count, board_x_size=args.board_x_size,
                        board_y_size=args.board_y_size, needed_symbols=args.needed_symbols)

    while True:
        client_socket, address = await server_socket.accept()
        print(f'Accepted a player - {client_socket} @ {address}')
        await curr_game_serv.add_player(client_socket, address)  # add the player to the server
        print(f'Current game server is hosting {len(curr_game_serv.players)}/{len(curr_game_serv.players)} players')
        if curr_game_serv.should_start():
            # Start the game and start accepting players for the next game
            print('Starting Game')
            await spawn(curr_game_serv.start_game)
            active_games.append(curr_game_serv)
            curr_game_serv = GameServer(player_count=args.player_count, board_x_size=args.board_x_size,
                                        board_y_size=args.board_y_size, needed_symbols=args.needed_symbols)
示例#10
0
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     await sleep(1)
     await sock.send(b'Msg')
     await sock.close()
示例#11
0
文件: test_io.py 项目: GMLudo/curio
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     s = sock.as_stream()
     await s.writelines([b'Msg1\n', b'Msg2\n', b'Msg3\n'])
     await sock.close()
     await serv.cancel()
示例#12
0
 async def canceller():
      task = await spawn(server(('',25000)))
      sock = socket(AF_INET, SOCK_STREAM)
      results.append('client connect')
      await sock.connect(('localhost', 25000))
      await sleep(1.0)
      await sock.close()
      results.append('client done')
示例#13
0
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     try:
         await sock.sendall(b'x' * 10000000)
     except CancelledError as e:
         results['sender'] = e.bytes_sent
     await sock.close()
示例#14
0
 async def canceller():
     task = await spawn(server(('', 25000)))
     sock = socket(AF_INET, SOCK_STREAM)
     results.append('client connect')
     await sock.connect(('localhost', 25000))
     await sleep(1.0)
     await sock.close()
     results.append('client done')
示例#15
0
文件: test_io.py 项目: GMLudo/curio
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     await sock.send(b'Message')
     data = await sock.recv(8192)
     await sock.close()
     await serv.cancel()
示例#16
0
 async def client(address):
     results.append('client start')
     sock = socket(AF_INET, SOCK_DGRAM)
     results.append('client send')
     await sock.sendto(b'Msg1', address)
     data, addr = await sock.recvfrom(8192)
     results.append(('client', data))
     await sock.close()
     results.append('client close')
示例#17
0
 async def client(address):
     results.append('client start')
     sock = socket(AF_INET, SOCK_DGRAM)
     results.append('client send')
     await sock.sendto(b'Msg1', address)
     data, addr = await sock.recvfrom(8192)
     results.append(('client', data))
     await sock.close()
     results.append('client close')
示例#18
0
 async def main():
     from curio.traps import _write_wait
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.bind(('', 26000))
     t = await spawn(reader(sock))
     await sleep(0.1)
     await _write_wait(sock.fileno())
     await t.cancel()
     await sock.close()
示例#19
0
async def fcgi_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    async with sock:
        while True:
            client, addr = await sock.accept()
            await spawn(fcgi_client(client, addr))
示例#20
0
 async def client(address):
     results.append("client start")
     sock = socket(AF_INET, SOCK_DGRAM)
     results.append("client send")
     await sock.sendto(b"Msg1", address)
     data, addr = await sock.recvfrom(8192)
     results.append(("client", data))
     await sock.close()
     results.append("client close")
示例#21
0
 async def canceller():
     accepting_event = Event()
     task = await spawn(server(("", 25000), accepting_event))
     await accepting_event.wait()
     sock = socket(AF_INET, SOCK_STREAM)
     results.append("client connect")
     await sock.connect(("localhost", 25000))
     await sleep(1.0)
     await sock.close()
     results.append("client done")
示例#22
0
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     s = sock.as_stream()
     try:
         await timeout_after(0.5, s.writelines(line_generator()))
     except TaskTimeout as e:
         results.append(e.bytes_written)
     await sock.close()
示例#23
0
async def socks5_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    print('Server listening at', address)
    async with sock:
        while True:
            client, addr = await sock.accept()
            await spawn(socks5_handle, client, addr, daemon=True)
示例#24
0
 async def server(addr):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(addr)
     sock.listen(1)
     await evt.set()
     client, addr = await sock.accept()
     assert addr[1] == 25001
     await client.close()
     await sock.close()
示例#25
0
 async def server(address):
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     results.append("recvfrom wait")
     data, addr = await sock.recvfrom(8192)
     results.append(("server", data))
     await sock.sendto(data, addr)
     await sock.close()
     results.append("server close")
示例#26
0
async def task(idx, msg, ip, port, send_interval, counters):
    sock = socket(AF_INET, SOCK_DGRAM)
    await sock.connect((ip, port))
    count = 0
    async with sock:
        while True:
            await sock.sendall(bytes.fromhex(msg))
            count += 1
            counters[idx] = count
            await sleep(send_interval)
示例#27
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(5)
     results.append('accept wait')
     client, addr = await sock.accept()
     results.append('accept done')
     await spawn(handler, client)
     await sock.close()
示例#28
0
 def server(host, port, context):
     sock = socket(AF_INET, SOCK_STREAM)
     try:
         sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
         sock.bind((host, port))
         sock.listen(5)
         return network.run_server(sock, handler, context)
     except Exception:
         sock._socket.close()
         raise
示例#29
0
 async def server(address):
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     results.append('recvfrom wait')
     data, addr = await sock.recvfrom(8192)
     results.append(('server', data))
     await sock.sendto(data, addr)
     await sock.close()
     results.append('server close')
示例#30
0
 async def canceller():
     accepting_event = Event()
     task = await spawn(server, ('', 25000), accepting_event)
     await accepting_event.wait()
     sock = socket(AF_INET, SOCK_STREAM)
     results.append('client connect')
     await sock.connect(('localhost', 25000))
     await sleep(1.0)
     await sock.close()
     results.append('client done')
示例#31
0
async def kv_server(kv_store_instance):
    key_value_store = kv_store_instance()
    server_socket = socket(AF_INET, SOCK_STREAM)
    server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    server_socket.bind((ADDRESS, PORT))
    server_socket.listen(5)
    async with server_socket:
        while True:
            (client_socket, client_address) = server_socket.accept()
            await spawn(kv_store_client, client_socket, client_address)
示例#32
0
async def echo_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    print('Server listening at', address)
    async with sock:
        while True:
            client, addr = await sock.accept()
            await spawn(iot_client(client, addr))
示例#33
0
 def server(host, port, context):
     sock = socket(AF_INET, SOCK_STREAM)
     try:
         sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
         sock.bind((host, port))
         sock.listen(5)
         return network.run_server(sock, handler, context)
     except Exception:
         sock._socket.close()
         raise
示例#34
0
async def echo_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    print(f'Server listening at {address}')
    async with sock:
        while True:
            client, addr = await sock.accept()
            await spawn(echo_client, client, addr)
示例#35
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(5)
     results.append("accept wait")
     client, addr = await sock.accept()
     results.append("accept done")
     await spawn(handler(client))
     await sock.close()
示例#36
0
 async def server(address):
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     results.append('recvfrom wait')
     try:
         await timeout_after(0.5, sock.recvfrom(8192))
         results.append('not here')
     except TaskTimeout:
         results.append('recvfrom timeout')
     await sock.close()
示例#37
0
async def echo_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    print('Server listening at', address)
    async with sock:
        while True:
             client, addr = await sock.accept()
             print('Connection from', addr)
             await new_task(echo_client(client))
示例#38
0
 async def canceller():
     accepting_event = Event()
     task = await spawn(server, ('', 25000), accepting_event)
     await accepting_event.wait()
     sock = socket(AF_INET, SOCK_STREAM)
     results.append('client connect')
     await sock.connect(('localhost', 25000))
     await sleep(1.0)
     await task.cancel()
     await sock.close()
     results.append('client done')
示例#39
0
 async def server(address):
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     results.append('recvfrom wait')
     try:
         await sock.recvfrom(8192)
         results.append('not here')
     except CancelledError:
         results.append('recvfrom cancel')
     await sock.close()
示例#40
0
 async def server(address):
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     results.append("recvfrom wait")
     try:
         await timeout_after(0.5, sock.recvfrom(8192))
         results.append("not here")
     except TaskTimeout:
         results.append("recvfrom timeout")
     await sock.close()
示例#41
0
 async def server(address):
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     results.append("recvfrom wait")
     try:
         await sock.recvfrom(8192)
         results.append("not here")
     except CancelledError:
         results.append("recvfrom cancel")
     await sock.close()
示例#42
0
async def echo_server(address):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    sock.bind(address)
    sock.listen(5)
    print('Server listening at', address)
    async with sock:
        while True:
            client, addr = await sock.accept()
            print('Connection from', addr)
            await new_task(echo_client(client))
示例#43
0
 async def test_client(address, serv):
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.recv(8)
     s = sock.as_stream()
     try:
         msg = b'x' * 10000000  # Must be big enough to fill buffers
         await timeout_after(0.5, s.write(msg))
     except TaskTimeout as e:
         results.append(e.bytes_written)
     await sock.close()
示例#44
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     results.append("accept wait")
     try:
         client, addr = await timeout_after(0.5, sock.accept())
         results.append("not here")
     except TaskTimeout:
         results.append("accept timeout")
     await sock.close()
示例#45
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     results.append('accept wait')
     try:
         client, addr = await timeout_after(0.5, sock.accept())
         results.append('not here')
     except TaskTimeout:
         results.append('accept timeout')
     await sock.close()
示例#46
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     results.append("accept wait")
     try:
         client, addr = await sock.accept()
         results.append("not here")
     except CancelledError:
         results.append("accept cancel")
     await sock.close()
示例#47
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     results.append('accept wait')
     try:
         client, addr = await sock.accept()
         results.append('not here')
     except CancelledError:
         results.append('accept cancel')
     await sock.close()
async def main(server_port):
    sock = socket(AF_INET, SOCK_STREAM)
    sock.bind(('', server_port))
    sock.listen(100)
    print("server listening on port %d" % server_port)

    # TODO: move this interrupt handling into client
    try:
        while True:
            client, addr = await sock.accept()
            await spawn(client_request(client, time.time()))
    except KeyboardInterrupt:
        pass  # exit
示例#49
0
 async def client(address):
     results.append('client start')
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.send(b'Msg1')
     await sleep(0.1)
     resp = await sock.recv(100)
     results.append(('client', resp))
     await sock.send(b'Msg2')
     await sleep(0.1)
     resp = await sock.recv(100)
     results.append(('client', resp))
     results.append('client close')
     await sock.close()
示例#50
0
 async def client(address):
     results.append("client start")
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.send(b"Msg1")
     await sleep(0.1)
     resp = await sock.recv(100)
     results.append(("client", resp))
     await sock.send(b"Msg2")
     await sleep(0.1)
     resp = await sock.recv(100)
     results.append(("client", resp))
     results.append("client close")
     await sock.close()
示例#51
0
 async def client(address):
     results.append('client start')
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     await sock.send(b'Msg1')
     await sleep(0.1)
     resp = await sock.recv(100)
     results.append(('client', resp))
     await sock.send(b'Msg2')
     await sleep(0.1)
     resp = await sock.recv(100)
     results.append(('client', resp))
     results.append('client close')
     await sock.close()
示例#52
0
 async def client(address):
     results.append("client start")
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     stream = sock.as_stream()
     await stream.write(b"Msg1\n")
     await sleep(0.1)
     resp = await stream.read(100)
     results.append(("client", resp))
     await stream.write(b"Msg2\n")
     await sleep(0.1)
     resp = await stream.read(100)
     results.append(("client", resp))
     results.append("client close")
     await sock.close()
示例#53
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     results.append('accept wait')
     client, addr = await sock.accept()
     results.append('recv wait')
     try:
         data = await timeout_after(0.5, client.recv(8192))
         results.append('not here')
     except TaskTimeout:
         results.append('recv timeout')
     await client.close()
     await sock.close()
示例#54
0
 async def server(address):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     results.append('accept wait')
     client, addr = await sock.accept()
     results.append('recv wait')
     try:
         data = await client.recv(8192)
         results.append('not here')
     except CancelledError:
         results.append('recv cancel')
     await client.close()
     await sock.close()
示例#55
0
 async def client(address):
     results.append('client start')
     sock = socket(AF_INET, SOCK_STREAM)
     await sock.connect(address)
     stream = sock.as_stream()
     await stream.write(b'Msg1\n')
     await sleep(0.1)
     resp = await stream.read(100)
     results.append(('client', resp))
     await stream.write(b'Msg2\n')
     await sleep(0.1)
     resp = await stream.read(100)
     results.append(('client', resp))
     results.append('client close')
     await sock.close()
示例#56
0
 async def server(address, accepting_event):
     sock = socket(AF_INET, SOCK_STREAM)
     sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, True)
     sock.bind(address)
     sock.listen(1)
     results.append('accept wait')
     await accepting_event.set()
     client, addr = await sock.accept()
     results.append('recv wait')
     try:
         data = await client.recv(8192)
         results.append('not here')
     except CancelledError:
         results.append('recv cancel')
     await client.close()
     await sock.close()