示例#1
0
 async def connect(self):
     # ssl_context = ssl.create_default_context()
     # ssl_context.verify_mode = ssl.CERT_REQUIRED
     # ssl_context.check_hostname = True  # redundant with match_hostname
     # self._socket = await open_connection(self.host, self.port, ssl=ssl)
     self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     await self._socket.connect((self.host, self.port))
     self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
     self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
     try:
         self._handshake.reset()
         response = None
         while True:
             request = self._handshake.next_message(response)
             if request is None:
                 break
             # This may happen in the `V1_0` protocol where we send two requests as
             # an optimization, then need to read each separately
             if request:
                 await self.sendall(request)
             # The response from the server is a null-terminated string
             response = (await self.read_until(b'\0'))[:-1]
     except (ReqlAuthError, ReqlTimeoutError):
         await self.close()
         raise
     except ReqlDriverError as ex:
         await self.close()
         error = str(ex)\
             .replace('receiving from', 'during handshake with')\
             .replace('sending to', 'during handshake with')
         raise ReqlDriverError(error)
     except socket.timeout as ex:
         await self.close()
         raise ReqlTimeoutError(self.host, self.port)
示例#2
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')
示例#3
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')
示例#4
0
async def echo_server(host, port):
    print(f'Echo server pid={os.getpid()}, listening at tcp://{host}:{port}')
    server = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    tcp_nodelay(server)
    server.bind((host, port))
    server.listen()
    while True:
        client, address = await server.accept()
        await spawn(echo_handler(client, address))
示例#5
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()
示例#6
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')
     async with timeout_after(0.1) as is_timeout:
         await sock.recvfrom(8192)
         results.append('not here')
     if is_timeout:
         results.append('recvfrom timeout')
     await sock.close()
示例#7
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(0.2)
     await task.cancel()
     await sock.close()
     results.append('client done')
示例#8
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')
     async with open_nursery() as nursery:
         await nursery.spawn(handler, client)
     await sock.close()
示例#9
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()
示例#10
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')
     async with timeout_after(0.1) as is_timeout:
         client, addr = await sock.accept()
         results.append('not here')
     if is_timeout:
         results.append('accept timeout')
     await sock.close()
示例#11
0
文件: server.py 项目: guyskk/weirb
 def _init_serv_sock(self):
     host = self.config.host
     port = self.config.port
     backlog = self.config.backlog
     serv_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     serv_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     serv_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
     flags = fcntl.fcntl(serv_sock.fileno(), fcntl.F_GETFD)
     flags |= fcntl.FD_CLOEXEC
     fcntl.fcntl(serv_sock.fileno(), fcntl.F_SETFD, flags)
     print(f"* Server PID={self._pid} listening at http://{host}:{port}")
     serv_sock.bind((host, port))
     serv_sock.listen(backlog)
     self._serv_sock = serv_sock
示例#12
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()