das ganze ist eigentlich nicht so richtig praktisch anwendbar, die kernidee ist aber glaub ich schon interesant
ueber feedback wuerde ich mich freuen

edit:
thx an BlackJack, Leonidas und andere die mir dafuer ein paar fragen beahtwortet haben



Code: Alles auswählen
from socket import *
from tempfile import gettempdir
from time import sleep
import sys
from urlparse import urlsplit
from os import system
from cgi import parse_qs
class HTMLGUI:
def __init__(self, defs, htaOpt = {}, host = "127.0.0.1", port = 80, maxClients = 5, title = ''):
s = socket(AF_INET, SOCK_STREAM)
s.bind((host, port))
s.listen(maxClients)
self.s = s
z = {}
for i in defs:
if i.__name__ == "index":
z["/"] = i
z["/" + i.__name__] = i
self.defs = z
print self.defs
html = '<html><head><title>' + title + '</title><hta:application \n'
for key, value in htaOpt.iteritems():
html += key + '="' + value + '"\n'
html += '></head><body><iframe frameborder="no" marginheight="0" marginwidth="0" height="100%" width="100%" src="http://127.0.0.1:' + str(port) + '"></body></html>'
file = gettempdir() + "\\start123.hta"
f = open(file, "w")
print "writing:", html
f.write(html)
f.close()
print system("start " + file)
def handleReq(self):
client, address = self.s.accept()
resp = client.recv(1024)
req = resp.splitlines()[0].split()[1]
z = urlsplit(req)#[2]
reqFile = z[2]; query = z[-2]
param = parse_qs(query)
z = {}
for i, i2 in param.iteritems():
z[i] = ''.join(i2)
param = z
print "param:", param, "requested file: ", reqFile
if reqFile in self.defs.keys():
self.defs[reqFile](param, client.send)
else:
print "bad request for", reqFile#; sys.exit(1)
client.send("file " + reqFile + " not found goto <a href=index>index</a>")
client.close()
return address, client #schadet sicher nicht
#--------------beispiel----------
def test(param, send):
send("abc")
def index(param, send):
send("hier index <a href=/test>test</a>")
def hello(param, send):
send("hello " + param["name"])
htmlMk = HTMLGUI(defs = [test, index, hello], title = "maria, maria i like it now...")
while 1:
htmlMk.handleReq()#threading oa einzubauen waere kp