├── .gitignore ├── LICENSE ├── README.md ├── asyncchat.py └── prime.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Will McGugan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # asyncchat 2 | Asynchronous Telnet Chat Server 3 | 4 | This is an example of using Python's `async` and `await` keywords *without* the use of asyncio or other framework. Requires Python3.6. 5 | 6 | Run the server with the following: 7 | 8 | ``` 9 | python3.6 asynchat.py 10 | ``` 11 | 12 | Then connect with telnet as follows: 13 | 14 | ``` 15 | telnet 127.0.0.1 2323 16 | ``` 17 | 18 | If you open multiple connections you will be able to send chat messages between clients. 19 | 20 | If you want to run the server on the internet, launch `asyncchat.py` as follows: 21 | 22 | ``` 23 | sudo python3.6 asynchat.py 0.0.0.0 23 24 | ``` 25 | 26 | Then you can connect with 27 | 28 | ```python 29 | telnet 30 | ``` 31 | -------------------------------------------------------------------------------- /asyncchat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3.6 2 | 3 | """ 4 | Asynchronous Chat Server 5 | ------------------------ 6 | 7 | This script runs a TCP/IP chat server that may be connected to via 8 | telnet. 9 | 10 | It uses Python's async and await keywords to serve multiple 11 | simultaneous client without the need for threads or processes. 12 | 13 | """ 14 | 15 | from collections import deque # doubled ended queue 16 | 17 | import select 18 | import socket 19 | 20 | # The Reader object is 'awaited' on in a coroutine, and tells the 21 | # Server instance to suspend the coroutine until there is data 22 | # available on the socket. 23 | class Reader: 24 | """An awaitable that reads from a socket.""" 25 | readers = set() # All readers awaiting IO 26 | 27 | def __init__(self, socket, max_bytes): 28 | self.socket = socket 29 | self.max_bytes = max_bytes 30 | 31 | def fileno(self): 32 | """Get file descriptor integer.""" 33 | # Allows this object to be used in 'select'. 34 | return self.socket.fileno() 35 | 36 | def __await__(self): 37 | self.readers.add(self) 38 | try: 39 | data = yield self # Get socket data from server 40 | return data # Return value of await statement 41 | finally: 42 | self.readers.discard(self) 43 | 44 | 45 | # The Writer object is 'awaited' on in a coroutine, and tells the 46 | # Server to suspend the coroutine until data can be sent. 47 | # In practice, sockets are almost always available to be written to, 48 | # but if network buffers are full we may have to wait for a send to 49 | # complete. 50 | class Writer: 51 | """An awaitable that writes to a socket.""" 52 | writers = set() # All writers awaiting IO 53 | 54 | def __init__(self, socket, data): 55 | self.socket = socket 56 | self.data = data 57 | 58 | def fileno(self): 59 | """Get file descriptor integer.""" 60 | return self.socket.fileno() 61 | 62 | def __await__(self): 63 | writers = self.writers 64 | # If something else is writing on that socket wait until 65 | # socket is free. 66 | while any(self.socket==socket for writer in writers): 67 | yield 68 | writers.add(self) 69 | try: 70 | # Write data in multiple batches if required. 71 | while self.data: 72 | sent_bytes = yield self # Wait for server to write data 73 | self.data = self.data[sent_bytes:] 74 | finally: 75 | writers.discard(self) 76 | 77 | 78 | # This Server object is essentially the 'loop' or the 'kernel' that 79 | # runs coroutines and waits for IO. 80 | class Server: 81 | """Run the server and the clients.""" 82 | 83 | def __init__(self, host, port): 84 | self.host = host 85 | self.port = port 86 | self._coros = deque() 87 | 88 | @classmethod 89 | def make_socket(cls, host, port): 90 | """Make a server socket.""" 91 | _socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 92 | _socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1) 93 | _socket.bind((host, port)) 94 | _socket.listen(1) 95 | return _socket 96 | 97 | @classmethod 98 | def accept(cls, server_socket): 99 | """Accept incoming connection.""" 100 | client_socket, client_address = server_socket.accept() 101 | connection = Connection(client_socket, client_address) 102 | return connection 103 | 104 | @classmethod 105 | def wait_for_io(cls): 106 | """Wait for any sockets available to read / write.""" 107 | rlist, wlist, _ = select.select( 108 | Reader.readers, 109 | Writer.writers, 110 | [], 111 | 60 112 | ) 113 | return rlist, wlist 114 | 115 | def add_coro(self, coro, send=None): 116 | """Add a coroutine to run on the next loop.""" 117 | self._coros.append((coro, send)) 118 | 119 | def _run_coroutines(self): 120 | """Run coroutines until all are awaiting or stalled.""" 121 | # Send stored value to coroutines and run them until the next 122 | # awaitable. If a coroutine yields None, it is 'stalled'. 123 | stalled = [] 124 | while self._coros: 125 | coro, send = self._coros.popleft() 126 | try: 127 | awaiting = coro.send(send) 128 | except StopIteration: 129 | # Coroutine completed. 130 | continue 131 | else: 132 | if awaiting is None: 133 | # Coroutine yielded with None, it is stalled. 134 | stalled.append((coro, None)) 135 | else: 136 | awaiting.coro = coro 137 | # Stalled coroutines may be able to run now. 138 | self._coros.extend(stalled) 139 | # Stash stalled coroutine for later. 140 | self._coros.extend(stalled) 141 | 142 | def run_forever(self): 143 | """Run the server forever (or until you press Ctrl+C).""" 144 | server_socket = self.make_socket(self.host, self.port) 145 | Reader.readers.add(server_socket) 146 | 147 | print('server running') 148 | print(f"connect with 'telnet {self.host} {self.port}'") 149 | 150 | # Note: this is analogous to an asyncio 'loop'. 151 | while True: 152 | self._run_coroutines() 153 | readers, writers = self.wait_for_io() 154 | 155 | for reader in readers: 156 | if reader is server_socket: 157 | # Pending new connection. 158 | connection = self.accept(server_socket) 159 | coro = connection.run() 160 | self.add_coro(coro) 161 | else: 162 | # Data available. Read it and send it to coroutine. 163 | data = reader.socket.recv(reader.max_bytes) 164 | self.add_coro(reader.coro, data) 165 | 166 | for writer in writers: 167 | # Socket may be written to, write data and send 168 | # bytes sent count to coroutine next cycle. 169 | bytes_sent = writer.socket.send(writer.data) 170 | self.add_coro(writer.coro, bytes_sent) 171 | 172 | 173 | class Connection: 174 | """A Chat client connection.""" 175 | chatters = set() 176 | 177 | def __init__(self, socket, client_address): 178 | self._socket = socket 179 | self.client_address = client_address 180 | self.name = None 181 | 182 | def recv(self, max_bytes): 183 | """Return a reader awaitable that reads data.""" 184 | return Reader(self._socket, max_bytes) 185 | 186 | def sendall(self, data): 187 | """Return a writer awaitable to send data.""" 188 | return Writer(self._socket, data) 189 | 190 | async def readline(self): 191 | """Coroutine to read a line.""" 192 | chars = [] 193 | while 1: 194 | char = await self.recv(1) 195 | chars.append(char) 196 | if char == b'\n' or char == b'': 197 | break 198 | return b''.join(chars).decode('ascii', 'replace') 199 | 200 | async def writeline(self, text): 201 | """Coroutine to write a line.""" 202 | await self.sendall(text.encode('ascii', 'replace') + b'\r\n') 203 | 204 | async def broadcast(self, text): 205 | """Coroutine to broadcast a line (send to other clients).""" 206 | print(text) # Let's us see whats going in in the server 207 | line_bytes = text.encode('ascii', 'replace') + b'\r\n' 208 | for connection in self.chatters: 209 | if connection is not self: 210 | await connection.sendall(line_bytes) 211 | 212 | async def run(self): 213 | """print connected / leave messages and run chat loop.""" 214 | print(f'{self.client_address} connected') 215 | try: 216 | await self.chat_loop() 217 | finally: 218 | print(f'{self.client_address} left') 219 | 220 | async def chat_loop(self): 221 | """Main loop of chat client.""" 222 | name = await self.get_user_name() 223 | await self.writeline(f'Welcome {name}!') 224 | await self.log_chatters() 225 | await self.broadcast(f'@{name} entered room') 226 | self.name = name 227 | self.chatters.add(self) 228 | try: 229 | while True: 230 | line = await self.readline() 231 | if line == '': 232 | break 233 | await self.broadcast(f'[{name}]\t{line.rstrip()}') 234 | finally: 235 | self.chatters.discard(self) 236 | await self.broadcast(f'@{name} left the room') 237 | 238 | async def log_chatters(self): 239 | """Tell the user who is online.""" 240 | names = sorted(chatter.name for chatter in self.chatters) 241 | for name in names: 242 | await self.writeline(f'@{name} is here') 243 | 244 | async def get_user_name(self): 245 | """Get a user name.""" 246 | await self.writeline('Please enter your name...') 247 | while True: 248 | name = await self.readline() 249 | name = name.strip().lower()[:16] 250 | if name not in self.chatters: 251 | break 252 | await self.writeline( 253 | 'That name is taken, please enter another...' 254 | ) 255 | return name 256 | 257 | 258 | if __name__ == "__main__": 259 | import sys 260 | if len(sys.argv) == 3: 261 | _, host, _port = sys.argv 262 | port = int(_port) 263 | else: 264 | host = '127.0.0.1' 265 | port = 2323 266 | server = Server(host, port) 267 | server.run_forever() 268 | -------------------------------------------------------------------------------- /prime.py: -------------------------------------------------------------------------------- 1 | 2 | def primes(): 3 | n = 2 4 | while True: 5 | if all(n % div for div in range(2, n)): 6 | yield n 7 | n += 1 8 | 9 | gen = primes() 10 | for n in range(20): 11 | print(next(gen)) 12 | --------------------------------------------------------------------------------