Created
April 10, 2015 17:07
-
-
Save Averroes/e29f18d78c369d60a8eb to your computer and use it in GitHub Desktop.
creating a tcp server
This file contains hidden or 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
| from socketserver import StreamRequestHandler, TCPServer | |
| class EchoHandler(StreamRequestHandler): | |
| def handle(self): | |
| print('Got connection from', self.client_address) | |
| # self.rfile is a file-like object for reading | |
| for line in self.rfile: | |
| # self.wfile is a file-like object for writing | |
| self.wfile.write(line) | |
| if __name__ == '__main__': | |
| import socket | |
| serv = TCPServer(('', 20000), EchoHandler, bind_and_activate=False) | |
| # Set up various socket options | |
| serv.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) | |
| # Bind and activate | |
| serv.server_bind() | |
| serv.server_activate() | |
| print('Echo server running on port 20000') | |
| serv.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment