Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 63 lines (51 sloc) 1.733 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
import gevent
from gevent import socket
import Tkinter as tk

class SockLoop(object):
    def __init__(self, callback):
        self.callback = callback

    def __call__(self, sock, client):
        while 1:
            mes = sock.recv(256)
            ret = self.callback(client, mes)
            if ret is not None:
                sock.send(ret)

def socket_server(port, callback):
    ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    ssock.bind(('', port))
    ssock.listen(5)

    while 1:
        sock, client = ssock.accept()
        gevent.spawn(callback, sock, client)

class App(object):
    def __init__(self, root):
        self.greenlet = None
        self.root = root
        self._build_window(root)
        self.root.after(100, self._connect)

    def add_text(self, text):
        cleaned_string = text.replace('\r', '')
        self.text.insert(tk.END, cleaned_string)

    def quit(self):
        self.root.quit()

    def _build_window(self, root):
        self.frame = tk.Frame(root)
        self.text = tk.Text(self.frame)
        self.quit_button = tk.Button(self.frame, text="Quit", command=self.quit)
        self.text.pack()
        self.quit_button.pack()
        self.frame.pack()

    def _connect(self):
        self.greenlet = gevent.spawn(
                socket_server,
                8080,
                SockLoop(lambda cl, txt: self.add_text("%s: %s" % (cl, txt))))
        self.gevent_loop_step()

    def gevent_loop_step(self):
        gevent.sleep()
        self.root.after_idle(self.gevent_loop_step)


if __name__ == '__main__':
    root = tk.Tk()
    app = App(root)
    root.mainloop()
Something went wrong with that request. Please try again.