Skip to content

Instantly share code, notes, and snippets.

@codersofthedark
Forked from robcowie/tornado_socket_server.py
Last active December 15, 2015 03:38
Show Gist options
  • Save codersofthedark/5195425 to your computer and use it in GitHub Desktop.
Save codersofthedark/5195425 to your computer and use it in GitHub Desktop.
import errno
import functools
import socket
from tornado import ioloop, web, httpserver, options
from couchbase.client import Couchbase
import json
def connection_ready(sock, fd, events):
while True:
try:
connection, address = sock.accept()
except socket.error, e:
if e[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
raise
return
else:
connection.setblocking(0)
CommunicationHandler(connection)
class CommunicationHandler():
"""Put your app logic here"""
def handle_data(self, data):
self.stream.write(data)
self._read()
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(0)
sock.bind(("", 8000))
sock.listen(128)
io_loop = ioloop.IOLoop.instance()
callback = functools.partial(connection_ready, sock)
io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
try:
io_loop.start()
except KeyboardInterrupt:
io_loop.stop()
print "exited cleanly"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment