A simple python tcp server
2007 November 8
# a simple tcp server
import SocketServer
class EchoRequestHandler(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, 'connected!'
self.request.send('hi ' + str(self.client_address) + '\n')
def handle(self):
data = 'dummy'
while data:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return
def finish(self):
print self.client_address, 'disconnected!'
self.request.send('bye ' + str(self.client_address) + '\n')
#server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 50008), EchoRequestHandler)
server.serve_forever()
Please put the code in a pre tag – without that the indentation is lost. This would not be a problem for most languages – but for python its an issue.
That’s correct. See this WordPress’ editor modify the text when switching between ‘html’ and ‘design’ modes.
I want to hate you now :\
As hard as I try all my python-network code ever returns is errors -_-
Die python die.
@James
I would love to give a hand and help you if you can share more info.
Long live python
Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.
fuck you yeah