Create using a convenient module for writing a server called SocketServer
#!/usr/bin/env python2.7
#-*- coding: utf-8 -*-
import SocketServer
HOST = '127.0.0.1'
PORT = 8080
class Handler(SocketServer.StreamRequestHandler):
def handle(self):
while True:
data = self.request.recv(1024)
print data
if len(data) == 0:
break
self.request.send(data)
self.request.close()
server = SocketServer.TCPServer((HOST, PORT), Handler)
print 'listening', server.socket.getsockname()
server.serve_forever()
Recommended Posts