Skip to content

Instantly share code, notes, and snippets.

@python273
Last active April 19, 2017 21:41
Show Gist options
  • Save python273/8e9cfcd62870f67e41c9acf273f3fe14 to your computer and use it in GitHub Desktop.
Save python273/8e9cfcd62870f67e41c9acf273f3fe14 to your computer and use it in GitHub Desktop.
import asyncio
@asyncio.coroutine
def proxify(reader, writer):
addr_writer = writer.get_extra_info('peername')[0]
while True:
data = yield from reader.read(8192)
if not data:
print('Close connection with {}'.format(addr_writer))
writer.close()
break
writer.write(data)
yield from w.drain()
@asyncio.coroutine
def handle_client(client_reader, client_writer):
print('A new connection')
server_reader, server_writer = yield from asyncio.open_connection(
'123.456.789.123', 443
)
asyncio.async(proxify(client_reader, server_writer))
asyncio.async(proxify(server_reader, client_writer))
def main():
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_client, '127.0.0.1', 443)
server = loop.run_until_complete(coro)
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment