locawapp-001.zip

LocaWapp rdjdvd at inwind.it
Sat Dec 10 15:11:47 EST 2005


LocaWapp: localhost web applications V.0.0.1 (2005 Dec 10)

Copyright (C) 2005 RDJ
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.

http://LocaWapp.blogspot.com
----------------------------


- Run with:

python run.py

- or:

python run.py 8081

- and browse with:

http://localhost:8080/locawapp/main.py

- or:

http://localhost:8081/locawapp/main.py

- If this is good for you, then you can help me developing with:

HTML + CSS + JS + PYTHON = LocaWapp
-----------------------------------


- Put your application in root:

[your_application]
[locawapp]
....__init__.py
....common.py
....main.py
....[static]
........logo.gif
........main.css
........main.js
README.TXT
run.py

- Your application must to have "init" and "main" (for convention):

[your_application]
....__init__.py
....main.py

- main.py is a web application, then it has "locawapp_main" function:

def locawapp_main(request):
[...]
return [string]

- See locawapp.main.py and locawapp.common.py

- Send me your comment, thanks :-)
- Bye

http://LocaWapp.blogspot.com
----------------------------


class Server:
        def __init__(self,port):
                self.resp = None
                self.session = {'_stopServer':False}

                s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                s.bind(('127.0.0.1',port))
                s.listen(5)
                while 1:
                        self.resp = ''
                        conn,addr = s.accept()
                        data = ''
                        while 1:
                                tmp = conn.recv(BUFFER)
                                if tmp == '':
                                        break
                                data += tmp
                                end = data.find('\r\n\r\n')
                                if end != -1:
                                        data2 =
data[:end].split('\r\n')
                                        method = data2[0].split(' ')
                                        method[1] =
urllib.unquote(method[1])
                                        header = {}
                                        for r in data2[1:]:
                                                rr = r.split(': ')
                                                header[rr[0]] = rr[1]
                                        if method[0] == 'GET':

self.getResp(method,header,None)
                                        elif method[0] == 'POST':
                                                cnt_len =
int(header['Content-Length'])
                                                content = data[end+4:]
                                                while len(content) <
cnt_len:
                                                        tmp =
conn.recv(BUFFER)
                                                        if tmp == '':
                                                                break
                                                        content += tmp
                                                content =
content[:cnt_len] # for MSIE 5.5 that append \r\n
                                                content =
urllib.unquote(content)

self.getResp(method,header,content)
                                        break
                        conn.send(self.resp)
                        conn.close()
                        if self.session['_stopServer']:
                                break
                s.close()

        def getResp(self,method,header,content):
                url = method[1].split('?')
                path = url[0][1:]
                ext = os.path.splitext(path)
                try:
                        if ext[1] == '.py':
                                get = {}
                                if len(url) == 2:
                                        get = self.getParams(url[1])
                                post = {}
                                if content != None:
                                        post = self.getParams(content)
                                request = {
                                        'header':header,
                                        'get':get,
                                        'post':post,
                                        'session':self.session
                                }
                                exec 'import ' +
ext[0].replace('/','.') + ' as app'
                                response = app.locawapp_main(request)
                                self.resp =
self.makeResp(200,'text/html',response['content'])
                        else:
                                try:
                                        f = open(path,'rb')
                                        fs = f.read()
                                        f.close()
                                        self.resp =
self.makeResp(200,self.getCntType(ext[1]),fs)
                                except IOError:
                                        self.resp =
self.makeResp(404,'text/html','404 Not Found')
                except Exception, e:
                        trace =
traceback.format_exception(sys.exc_type,sys.exc_value,sys.exc_traceback)
                        self.resp =
self.makeResp(200,'text/html',self.getError(trace))




More information about the Python-list mailing list