Created
May 13, 2016 19:48
-
-
Save carrotIndustries/aefd4552745096ac47ce4f920b0958e1 to your computer and use it in GitHub Desktop.
segfaults on win32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from gi.repository import GObject, GLib | |
#based on http://rox.sourceforge.net/desktop/node/413.html | |
def handler(conn, flags, sock): | |
'''Asynchronous connection handler. Processes each line from the socket.''' | |
line = sock.recv(4096) | |
if not len(line): | |
print("Connection closed.") | |
return False | |
else: | |
print(line) | |
return True | |
def listener(conn, flags, sock): | |
'''Asynchronous connection listener. Starts a handler for each connection.''' | |
conn, addr = sock.accept() | |
print("Connected") | |
GObject.io_add_watch(conn, GObject.IO_IN, handler, conn) | |
return True | |
msock = socket.socket() | |
msock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
msock.bind(("localhost", 1337)) | |
msock.listen(1) | |
print("Listening...") | |
GObject.io_add_watch(msock, GObject.IO_IN, listener, msock) | |
GObject.MainLoop().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment