Skip to content

Instantly share code, notes, and snippets.

@XericZephyr
Created November 14, 2015 08:06
Show Gist options
  • Save XericZephyr/b11e829e5ed6bd189d60 to your computer and use it in GitHub Desktop.
Save XericZephyr/b11e829e5ed6bd189d60 to your computer and use it in GitHub Desktop.
Simple TCP Server For print http request
import SocketServer
import argparse
class MyTCPHandler(SocketServer.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(65535).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
# just send back the same data, but upper-cased
# self.request.sendall(self.data.upper())
def main():
parser = argparse.ArgumentParser(description='A simple tcp server, just print out the text input request')
parser.add_argument('--host', type=str, default="localhost",
help ='The host, DEFAULT: localhost')
parser.add_argument('--port', type=int, default=8080,
help='The port number, DEFAULT: 8080')
args = parser.parse_args()
server = SocketServer.TCPServer((args.host, args.port), MyTCPHandler)
server.serve_forever()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment