Changeset 2762 for package_branches
- Timestamp:
- Dec 21, 2009, 1:12:08 AM (15 years ago)
- Location:
- package_branches/invirt-web/ajaxterm-rebased/code
- Files:
-
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
package_branches/invirt-web/ajaxterm-rebased/code/ajaxterm.py
r2761 r2762 3 3 """ Ajaxterm """ 4 4 5 import array,cgi,fcntl,glob,hashlib,mimetypes,optparse,os,pty,random,re,signal,select,sys,threading,time,termios,struct,pwd 6 7 os.chdir(os.path.normpath(os.path.dirname(__file__))) 8 # Optional: Add QWeb in sys path 9 sys.path[0:0]=glob.glob('../../python') 10 11 import qweb 5 import array,atexit,cgi,fcntl,glob,hashlib,mimetypes,optparse,os,pty,random,re,signal,select,sys,threading,time,termios,struct,pwd 12 6 13 7 class Terminal: … … 372 366 self.thread.daemon=True 373 367 self.alive=1 368 atexit.register(self.die) 374 369 # synchronize methods 375 370 for name in ['create','fds','proc_read','proc_write','dump','die','run']: … … 454 449 except (IOError,OSError): 455 450 pass 456 457 class AjaxTerm:458 def __init__(self,cmd=None,index_file='ajaxterm.html'):459 self.files={}460 for i in ['css','html','js']:461 for j in glob.glob('*.%s'%i):462 self.files[j]=file(j).read()463 self.files['index']=file(index_file).read()464 self.mime = mimetypes.types_map.copy()465 self.mime['.html']= 'text/html; charset=UTF-8'466 self.multi = Multiplex(cmd)467 self.session = {}468 def __call__(self, environ, start_response):469 req = qweb.QWebRequest(environ, start_response,session=None)470 if req.PATH_INFO.endswith('/u'):471 s=req.REQUEST["s"]472 k=req.REQUEST["k"]473 c=req.REQUEST["c"]474 w=req.REQUEST.int("w")475 h=req.REQUEST.int("h")476 if s in self.session:477 term=self.session[s]478 else:479 if not (w>2 and w<256 and h>2 and h<100):480 w,h=80,25481 term=self.session[s]=self.multi.create(w,h)482 if k:483 self.multi.proc_write(term,k)484 time.sleep(0.002)485 dump=self.multi.dump(term,c)486 req.response_headers['Content-Type']='text/xml'487 if isinstance(dump,str):488 req.write(dump)489 req.response_gzencode=1490 else:491 del self.session[s]492 req.write('<?xml version="1.0"?><idem></idem>')493 # print "sessions %r"%self.session494 else:495 n=os.path.basename(req.PATH_INFO)496 if n in self.files:497 req.response_headers['Content-Type'] = self.mime.get(os.path.splitext(n)[1].lower(), 'application/octet-stream')498 req.write(self.files[n])499 else:500 req.response_headers['Content-Type'] = 'text/html; charset=UTF-8'501 req.write(self.files['index'])502 return req503 504 def main():505 parser = optparse.OptionParser()506 parser.add_option("-p", "--port", dest="port", default="8022", help="Set the TCP port (default: 8022)")507 parser.add_option("-c", "--command", dest="cmd", default=None,help="set the command (default: /bin/login or ssh localhost)")508 parser.add_option("-l", "--log", action="store_true", dest="log",default=0,help="log requests to stderr (default: quiet mode)")509 parser.add_option("-d", "--daemon", action="store_true", dest="daemon", default=0, help="run as daemon in the background")510 parser.add_option("-P", "--pidfile",dest="pidfile",default="/var/run/ajaxterm.pid",help="set the pidfile (default: /var/run/ajaxterm.pid)")511 parser.add_option("-i", "--index", dest="index_file", default="ajaxterm.html",help="default index file (default: ajaxterm.html)")512 parser.add_option("-u", "--uid", dest="uid", help="Set the daemon's user id")513 (o, a) = parser.parse_args()514 if o.daemon:515 pid=os.fork()516 if pid == 0:517 #os.setsid() ?518 os.setpgrp()519 nullin = file('/dev/null', 'r')520 nullout = file('/dev/null', 'w')521 os.dup2(nullin.fileno(), sys.stdin.fileno())522 os.dup2(nullout.fileno(), sys.stdout.fileno())523 os.dup2(nullout.fileno(), sys.stderr.fileno())524 if os.getuid()==0 and o.uid:525 try:526 os.setuid(int(o.uid))527 except:528 os.setuid(pwd.getpwnam(o.uid).pw_uid)529 else:530 try:531 file(o.pidfile,'w+').write(str(pid)+'\n')532 except:533 pass534 print 'AjaxTerm at http://localhost:%s/ pid: %d' % (o.port,pid)535 sys.exit(0)536 else:537 print 'AjaxTerm at http://localhost:%s/' % o.port538 at=AjaxTerm(o.cmd,o.index_file)539 # f=lambda:os.system('firefox http://localhost:%s/&'%o.port)540 # qweb.qweb_wsgi_autorun(at,ip='localhost',port=int(o.port),threaded=0,log=o.log,callback_ready=None)541 try:542 qweb.QWebWSGIServer(at,ip='localhost',port=int(o.port),threaded=0,log=o.log).serve_forever()543 except KeyboardInterrupt,e:544 sys.excepthook(*sys.exc_info())545 at.multi.die()546 547 if __name__ == '__main__':548 main()549
Note: See TracChangeset
for help on using the changeset viewer.