Created
September 29, 2011 22:47
-
-
Save jpic/1252156 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 sys | |
| from select import select | |
| import socket | |
| import readline | |
| # HACK - python doesn't include a binding to rl_callback_read_char | |
| import readline | |
| import ctypes | |
| rl_lib = ctypes.cdll.LoadLibrary("libreadline.so") | |
| readline.callback_handler_remove = rl_lib.rl_callback_handler_remove | |
| readline.callback_read_char = rl_lib.rl_callback_read_char | |
| # the callback needs special treatment: | |
| rlcallbackfunctype = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p) | |
| def setcallbackfunc(prompt, thefunc): | |
| rl_lib.rl_callback_handler_install(prompt, rlcallbackfunctype(thefunc)) | |
| readline.callback_handler_install = setcallbackfunc | |
| # ENDHACK | |
| PROMPT = 'fics> ' | |
| def erase_prompt(): | |
| print(chr(8)*len(PROMPT)) | |
| def print_prompt(): | |
| readline.callback_handler_remove() | |
| setcallbackfunc(PROMPT, process_input) | |
| def process_input(input): | |
| input = input.strip() | |
| if not len(input): | |
| return | |
| erase_prompt() | |
| requests.append(input) | |
| print_prompt() | |
| print_prompt() | |
| requests = [] | |
| fics = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| fics.connect(('freechess.org', 5000)) | |
| while True: | |
| r, w, x = select([sys.stdin, fics], [fics], []) | |
| if fics in w: | |
| for request in requests: | |
| fics.send(request) | |
| if fics in r: | |
| erase_prompt() | |
| print fics.recvfrom(16384)[0] | |
| print_prompt() | |
| if sys.stdin in r: | |
| readline.callback_read_char() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment