File: Karrigell_GUI.py

package info (click to toggle)
karrigell 2.3.2-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,484 kB
  • ctags: 4,167
  • sloc: python: 18,122; ansic: 370; sh: 129; makefile: 53; xml: 29
file content (80 lines) | stat: -rw-r--r-- 2,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Karrigell HTTP Server

Written by Pierre Quentel quentel.pierre@wanadoo.fr

Published under the BSD licence. See the file LICENCE.txt

This script launches Karrigell with webservers.SimpleAsyncHTTPServer.Server as web server.
It is built on the asyncore/asynchat framework (non-blocking sockets, use of
the select() function) and partly copied from the medusa web server

References :
- medusa : http://www.amk.ca/python/code/medusa.html for medusa
- Sockets HOWTO on www.python.org

Requests are handled by class RequestHandler (one instance per request)
"""

import webservers.SimpleAsyncHTTPServer
import KarrigellRequestHandler
import k_config
import traceback
import sys

class asyncRequestHandler(webservers.SimpleAsyncHTTPServer.DialogManager,
    KarrigellRequestHandler.KarrigellRequestHandler):
    
    def handle_data(self):
        KarrigellRequestHandler.KarrigellRequestHandler.handle_data(self)
    
    def send_error(self, code, message=None):
        KarrigellRequestHandler.KarrigellRequestHandler.send_error(self,code,message)

    def handle_error(self):
        traceback.print_exc(file=sys.stderr)

# Launch the server

s=webservers.SimpleAsyncHTTPServer.Server(('',k_config.port),asyncRequestHandler)
print "Karrigell %s running on port %s" %(KarrigellRequestHandler.__version__,k_config.port)
if k_config.debug:
    print "Debug level %s" %k_config.debug
if k_config.silent:
    print "Silent mode"

import thread
# start the server in a different thread
thread.start_new_thread(s.loop, ())

# GUI to stop the server and log
from Tkinter import *
from ScrolledText import ScrolledText
import tkFont

class Output:

    maxlines = 100

    def __init__(self,textWidget):
        self.textWidget = textWidget
    
    def write(self,data):
        self.textWidget.insert(END,data)
        l = int(self.textWidget.index(END).split('.')[0])
        if l > self.maxlines:
            self.textWidget.delete(1.0,'%s.0' %(l-self.maxlines))
        self.textWidget.see(END)        

def stop_server():
    s.close_all()
    sys.exit()

root = Tk()
Button(root,text="Stop server",command = stop_server).pack()
tw = ScrolledText(root,width=80,height=40,bg="black",foreground="white",
    font=tkFont.Font(family="courier",size=10,weight="bold"))
tw.pack()

sys.stderr = Output(tw)

root.mainloop()