[svn r37264] create the new development trunk

--HG--
branch : trunk
This commit is contained in:
hpk
2007-01-24 15:24:01 +01:00
commit 5992a8ef21
435 changed files with 58640 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
"""
send a "quit" signal to a remote server
"""
import sys
import socket
hostport = sys.argv[1]
host, port = hostport.split(':')
hostport = (host, int(port))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(hostport)
sock.sendall('"raise KeyboardInterrupt"\n')

84
py/execnet/script/shell.py Executable file
View File

@@ -0,0 +1,84 @@
#! /usr/bin/env python
"""
a remote python shell
for injection into startserver.py
"""
import sys, os, socket, select
try:
clientsock
except NameError:
print "client side starting"
import sys
host, port = sys.argv[1].split(':')
port = int(port)
myself = open(os.path.abspath(sys.argv[0]), 'rU').read()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
sock.sendall(repr(myself)+'\n')
print "send boot string"
inputlist = [ sock, sys.stdin ]
try:
while 1:
r,w,e = select.select(inputlist, [], [])
if sys.stdin in r:
line = raw_input()
sock.sendall(line + '\n')
if sock in r:
line = sock.recv(4096)
sys.stdout.write(line)
sys.stdout.flush()
except:
import traceback
print traceback.print_exc()
sys.exit(1)
print "server side starting"
# server side
#
from traceback import print_exc
from threading import Thread
class promptagent(Thread):
def __init__(self, clientsock):
Thread.__init__(self)
self.clientsock = clientsock
def run(self):
print "Entering thread prompt loop"
clientfile = self.clientsock.makefile('w')
filein = self.clientsock.makefile('r')
loc = self.clientsock.getsockname()
while 1:
try:
clientfile.write('%s %s >>> ' % loc)
clientfile.flush()
line = filein.readline()
if len(line)==0: raise EOFError,"nothing"
#print >>sys.stderr,"got line: " + line
if line.strip():
oldout, olderr = sys.stdout, sys.stderr
sys.stdout, sys.stderr = clientfile, clientfile
try:
try:
exec compile(line + '\n','<remote pyin>', 'single')
except:
print_exc()
finally:
sys.stdout=oldout
sys.stderr=olderr
clientfile.flush()
except EOFError,e:
print >>sys.stderr, "connection close, prompt thread returns"
break
#print >>sys.stdout, "".join(apply(format_exception,sys.exc_info()))
self.clientsock.close()
prompter = promptagent(clientsock)
prompter.start()
print "promptagent - thread started"

View File

@@ -0,0 +1,87 @@
#! /usr/bin/env python
"""
start socket based minimal readline exec server
"""
# this part of the program only executes on the server side
#
progname = 'socket_readline_exec_server-1.2'
debug = 0
import sys, socket, os
try:
import fcntl
except ImportError:
fcntl = None
if debug: # and not os.isatty(sys.stdin.fileno()):
f = open('/tmp/execnet-socket-pyout.log', 'a', 0)
old = sys.stdout, sys.stderr
sys.stdout = sys.stderr = f
#import py
#compile = py.code.compile
def exec_from_one_connection(serversock):
print progname, 'Entering Accept loop', serversock.getsockname()
clientsock,address = serversock.accept()
print progname, 'got new connection from %s %s' % address
clientfile = clientsock.makefile('r+',0)
print "reading line"
# rstrip so that we can use \r\n for telnet testing
source = clientfile.readline().rstrip()
clientfile.close()
g = {'clientsock' : clientsock, 'address' : address}
source = eval(source)
if source:
co = compile(source+'\n', source, 'exec')
print progname, 'compiled source, executing'
try:
exec co in g
finally:
print progname, 'finished executing code'
# background thread might hold a reference to this (!?)
#clientsock.close()
def bind_and_listen(hostport):
if isinstance(hostport, str):
host, port = hostport.split(':')
hostport = (host, int(port))
serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set close-on-exec
if hasattr(fcntl, 'FD_CLOEXEC'):
old = fcntl.fcntl(serversock.fileno(), fcntl.F_GETFD)
fcntl.fcntl(serversock.fileno(), fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
# allow the address to be re-used in a reasonable amount of time
if os.name == 'posix' and sys.platform != 'cygwin':
serversock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversock.bind(hostport)
serversock.listen(5)
return serversock
def startserver(serversock, loop=False):
try:
while 1:
try:
exec_from_one_connection(serversock)
except (KeyboardInterrupt, SystemExit):
raise
except:
import traceback
traceback.print_exc()
if not loop:
break
finally:
print "leaving socketserver execloop"
serversock.shutdown(2)
if __name__ == '__main__':
import sys
if len(sys.argv)>1:
hostport = sys.argv[1]
else:
hostport = ':8888'
serversock = bind_and_listen(hostport)
startserver(serversock, loop=True)

View File

@@ -0,0 +1,91 @@
"""
A windows service wrapper for the py.execnet socketserver.
To use, run:
python socketserverservice.py register
net start ExecNetSocketServer
"""
import sys
import os
import time
import win32serviceutil
import win32service
import win32event
import win32evtlogutil
import servicemanager
import threading
import socketserver
appname = 'ExecNetSocketServer'
class SocketServerService(win32serviceutil.ServiceFramework):
_svc_name_ = appname
_svc_display_name_ = "%s" % appname
_svc_deps_ = ["EventLog"]
def __init__(self, args):
# The exe-file has messages for the Event Log Viewer.
# Register the exe-file as event source.
#
# Probably it would be better if this is done at installation time,
# so that it also could be removed if the service is uninstalled.
# Unfortunately it cannot be done in the 'if __name__ == "__main__"'
# block below, because the 'frozen' exe-file does not run this code.
#
win32evtlogutil.AddSourceToRegistry(self._svc_display_name_,
servicemanager.__file__,
"Application")
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.WAIT_TIME = 1000 # in milliseconds
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
# Redirect stdout and stderr to prevent "IOError: [Errno 9]
# Bad file descriptor". Windows services don't have functional
# output streams.
sys.stdout = sys.stderr = open('nul', 'w')
# Write a 'started' event to the event log...
win32evtlogutil.ReportEvent(self._svc_display_name_,
servicemanager.PYS_SERVICE_STARTED,
0, # category
servicemanager.EVENTLOG_INFORMATION_TYPE,
(self._svc_name_, ''))
print "Begin: %s" % (self._svc_display_name_)
hostport = ':8888'
print 'Starting py.execnet SocketServer on %s' % hostport
serversock = socketserver.bind_and_listen(hostport)
thread = threading.Thread(target=socketserver.startserver,
args=(serversock,),
kwargs={'loop':True})
thread.setDaemon(True)
thread.start()
# wait to be stopped or self.WAIT_TIME to pass
while True:
result = win32event.WaitForSingleObject(self.hWaitStop,
self.WAIT_TIME)
if result == win32event.WAIT_OBJECT_0:
break
# write a 'stopped' event to the event log.
win32evtlogutil.ReportEvent(self._svc_display_name_,
servicemanager.PYS_SERVICE_STOPPED,
0, # category
servicemanager.EVENTLOG_INFORMATION_TYPE,
(self._svc_name_, ''))
print "End: %s" % appname
if __name__ == '__main__':
# Note that this code will not be run in the 'frozen' exe-file!!!
win32serviceutil.HandleCommandLine(SocketServerService)

9
py/execnet/script/xx.py Normal file
View File

@@ -0,0 +1,9 @@
import rlcompleter2
rlcompleter2.setup()
import register, sys
try:
hostport = sys.argv[1]
except:
hostport = ':8888'
gw = register.ServerGateway(hostport)