Created
September 10, 2018 23:49
Multiconn server that handles BlockingIOError exception.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import socket | |
import selectors | |
import types | |
sel = selectors.DefaultSelector() | |
def accept_wrapper(sock): | |
conn, addr = sock.accept() # Should be ready to read | |
print('accepted connection from', addr) | |
conn.setblocking(False) | |
data = types.SimpleNamespace(addr=addr, inb=b'', outb=b'') | |
events = selectors.EVENT_READ | selectors.EVENT_WRITE | |
sel.register(conn, events, data=data) | |
def service_connection(key, mask): | |
sock = key.fileobj | |
data = key.data | |
if mask & selectors.EVENT_READ: | |
try: | |
recv_data = sock.recv(1024) # Should be ready to read | |
except BlockingIOError: | |
# Resource temporarily unavailable (errno EWOULDBLOCK) | |
pass | |
else: | |
if recv_data: | |
data.outb += recv_data | |
else: | |
print('closing connection to', data.addr) | |
sel.unregister(sock) | |
sock.close() | |
if mask & selectors.EVENT_WRITE: | |
if data.outb: | |
print('echoing', repr(data.outb), 'to', data.addr) | |
try: | |
sent = sock.send(data.outb) # Should be ready to write | |
except BlockingIOError: | |
# Resource temporarily unavailable (errno EWOULDBLOCK) | |
pass | |
else: | |
data.outb = data.outb[sent:] | |
if len(sys.argv) != 3: | |
print('usage:', sys.argv[0], '<host> <port>') | |
sys.exit(1) | |
host, port = sys.argv[1], int(sys.argv[2]) | |
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
lsock.bind((host, port)) | |
lsock.listen() | |
print('listening on', (host, port)) | |
lsock.setblocking(False) | |
sel.register(lsock, selectors.EVENT_READ, data=None) | |
try: | |
while True: | |
events = sel.select(timeout=None) | |
for key, mask in events: | |
if key.data is None: | |
accept_wrapper(key.fileobj) | |
else: | |
service_connection(key, mask) | |
except KeyboardInterrupt: | |
print('caught keyboard interrupt, exiting') | |
finally: | |
sel.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
I want to develop a network that contain One Server and 10 clients and want to do multi-hop communication (Means, with server only or two client directly connected and other will be send data through multi-hop client)
I want duplex communication (Both way at a same time) at each node (All Clients and server)
In your above server program, we need to give client ip and port that will connect with only that cleint where other clients can not communicate with server and also other cleint can not communicate with this client which is connect with server.
Can you upload both client and server program in which server and all client could communicate with each other in duplex mode (sending, receving and forwaring data to next hop) ?
Client program should be one which could execute on all client node...
Please help me.