Skip to content

Instantly share code, notes, and snippets.

@sma
Created December 14, 2008 11:16
Show Gist options
  • Save sma/35667 to your computer and use it in GitHub Desktop.
Save sma/35667 to your computer and use it in GitHub Desktop.
remote Python console
#!/usr/bin/env python
from xmlrpclib import ServerProxy
client = ServerProxy('http://localhost:8877')
while True:
try:
line = raw_input(">>> ")
if line:
status, result = client.execute(line + "\n")
if status == 0:
while True:
cont = raw_input("... ")
if not cont:
break
line += "\n" + cont
status, result = client.execute(line + "\n")
if result:
print result,
except (KeyboardInterrupt, EOFError):
print
break
#!/usr/bin/env python
import sys, traceback
from StringIO import StringIO
from SimpleXMLRPCServer import SimpleXMLRPCServer
g = {}
def execute(s):
try:
code = compile(s, "<repl>", "single")
except SyntaxError:
traceback.print_exc()
return 0, ""
try:
sys.stdout = sys.stderr = StringIO()
eval(code, g)
return 1, sys.stdout.getvalue()
except:
t, v, tb = sys.exc_info()
traceback.print_exception(t, v, tb.tb_next)
return 2, sys.stdout.getvalue()
finally:
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
server = SimpleXMLRPCServer(('localhost', 8877), logRequests=False)
server.register_function(execute)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment