I am trying to understand how we can use
coroutines to write asynchronous code in a sequential style without using any of the new features in Python so that the fundamental idea is clear.
Is this approach fine? One thing I can probably improve upon is the idea of using other
coroutines. Is there any feedback on this code?
#!/usr/bin/python import socket import selectors select = selectors.DefaultSelector() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) hostname = socket.gethostname() print("Server starting on port 9000") s.bind((hostname, 9000)) s.listen(5) class Deferred(object): def __init__(self): self.state = "pending" self.value = None self.task = None def done(self, value): self.state = "complete" self.value = value if self.task: self.task(value) def future(self, task): if self.state == "complete": self.task(value) else: self.task = task def watch(fd, event): deferred = Deferred() select.register(fd, event, deferred.done) return deferred def unwatch(fd): select.unregister(fd) def run_till_complete(g): try: d = g.send(None) d.future(lambda value: run_till_complete(g)) except StopIteration: pass def handle_client(s): c, a = s.accept() message = "Connection recieved from %s" % str(a) print(message) run_till_complete(echo_client(c)) def echo_client(c): while True: yield watch(c, selectors.EVENT_READ) unwatch(c) r = c.recv(1024) yield watch(c, selectors.EVENT_WRITE) unwatch(c) c.send(bytes(r)) select.register(s, selectors.EVENT_READ, handle_client) try: while True: events = select.select() for key, mask in events: callback = key.data callback(key.fileobj) except KeyboardInterrupt: print(" recieved") finally: print("Server stopping on port 9000") select.unregister(s) s.close()
A simple client looks like this:
#!/usr/bin/python import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) hostname = socket.gethostname() client_socket.connect((hostname, 9000)) chunks =  while True: message = bytes(input("Message: "), 'utf-8') client_socket.send(message) print(client_socket.recv(1024))
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!