Skip to content

Instantly share code, notes, and snippets.

@rms1000watt
Created September 6, 2016 21:14
Show Gist options
  • Save rms1000watt/4108bf0e9bf8b1cfed57136b2ce14509 to your computer and use it in GitHub Desktop.
Save rms1000watt/4108bf0e9bf8b1cfed57136b2ce14509 to your computer and use it in GitHub Desktop.
Python Tornado TCP Server
# TAKEN FROM: http://alexapps.net/python-tornado-simple-tcp-server
import socket
import tornado.gen
import tornado.ioloop
import tornado.iostream
import tornado.tcpserver
class SimpleTcpClient(object):
client_id = 0
def __init__(self, stream):
super().__init__()
SimpleTcpClient.client_id += 1
self.id = SimpleTcpClient.client_id
self.stream = stream
self.stream.socket.setsockopt(
socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.stream.socket.setsockopt(
socket.IPPROTO_TCP, socket.SO_KEEPALIVE, 1)
self.stream.set_close_callback(self.on_disconnect)
@tornado.gen.coroutine
def on_disconnect(self):
self.log("disconnected")
yield []
@tornado.gen.coroutine
def dispatch_client(self):
try:
while True:
line = yield self.stream.read_until(b'\n')
self.log('got |%s|' % line.decode('utf-8').strip())
yield self.stream.write(line)
except tornado.iostream.StreamClosedError:
pass
@tornado.gen.coroutine
def on_connect(self):
raddr = 'closed'
try:
raddr = '%s:%d' % self.stream.socket.getpeername()
except Exception:
pass
self.log('new, %s' % raddr)
yield self.dispatch_client()
def log(self, msg, *args, **kwargs):
print('[connection %d] %s' % (self.id, msg.format(*args, **kwargs)))
class SimpleTcpServer(tornado.tcpserver.TCPServer):
@tornado.gen.coroutine
def handle_stream(self, stream, address):
"""
Called for each new connection, stream.socket is
a reference to socket object
"""
connection = SimpleTcpClient(stream)
yield connection.on_connect()
def main():
# configuration
host = '0.0.0.0'
port = 8008
# tcp server
server = SimpleTcpServer()
server.listen(port, host)
print("Listening on %s:%d..." % (host, port))
# infinite loop
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
@MilenaHernandez
Copy link

tengo una pregunta. como recibes la informacion que envia un cliente que se conecta?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment