Karrigell + PyProcessing,Very good and Strong!

16 views
Skip to first unread message

ccnusjy

unread,
Aug 23, 2008, 6:14:38 AM8/23/08
to karrigell
import os
import traceback
import sys

import KarrigellRequestHandler
import k_config, k_utils
import webservers.SimpleAsyncHTTPServer as Server
from processing import Process, currentProcess, freezeSupport
if sys.platform == 'win32':
import processing.reduction # make sockets pickable/inheritable

class
asyncRequestHandler(KarrigellRequestHandler.KarrigellRequestHandler,
Server.DialogManager):

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)

def kkk_serve(s):
#print k_config.silent
#if k_config.silent:
sys.stdout = k_utils.silent()
sys.stderr = k_utils.silent()
try:
s.loop()
except KeyboardInterrupt:
s.close_all()
k_utils.trace("Ctrl+C pressed. Shutting down.")

def main_start():
# Launch the server
port = 8086
process_num = 3

s = Server.Server(('localhost',port),asyncRequestHandler)

print "Karrigell %s running on port %s" %
(KarrigellRequestHandler.__version__,
port)
print "Press Ctrl+C to stop"

for i in range(process_num):
child = Process(target=kkk_serve, args=(s,))
child.setDaemon(True)
child.start()

kkk_serve(s)

if __name__ == '__main__':
if sys.platform == 'win32':
freezeSupport()
main_start()

Harlin Seritt

unread,
Aug 23, 2008, 6:18:05 AM8/23/08
to karr...@googlegroups.com
Hi,

Thanks for the code! Would you mind adding some documentation and a good summary of what this code provides? I know I would appreciate it very much.

Thanks!

Harlin Seritt

--- On Sat, 8/23/08, ccnusjy <ccn...@gmail.com> wrote:

ccnusjy

unread,
Aug 23, 2008, 7:38:46 AM8/23/08
to karrigell
Hi, you can put the script above to a file called
Karrgiell_multiprocess.py , and then double click it to run Karrigell
Server with many process instances which are all running at the same
tcp port.

As we all know, Karrigell handle custom's request one by one, so one a
long-running script is executing, other request seams to be blocked .

Now, there is no problem about that with the power of pyprocessing


> def main_start():
> # Launch the server
> port = 8086
#~~~~~~~~~~~~~~ ,here is the port your server will listen to
> process_num = 3
#~~~~~~~~~~~~~~, here is how many process instances to serve
>
> s = Server.Server(('localhost',port),asyncRequestHandler)

On Aug 23, 6:18 pm, Harlin Seritt <harlinser...@yahoo.com> wrote:
> Hi,
>
> Thanks for the code! Would you mind adding some documentation and a good summary of what this code provides? I know I would appreciate it very much.
>
> Thanks!
>
> Harlin Seritt
>
> --- On Sat, 8/23/08, ccnusjy <ccnu...@gmail.com> wrote:
> From: ccnusjy <ccnu...@gmail.com>

Harlin Seritt

unread,
Aug 23, 2008, 11:44:34 AM8/23/08
to karr...@googlegroups.com
Thanks ccnusjy! Pierre, is this something you're going to include in the Karrigell 3.0?

Thanks,

Harlin Seritt

--- On Sat, 8/23/08, ccnusjy <ccn...@gmail.com> wrote:

Graham Dumpleton

unread,
Aug 24, 2008, 2:07:48 AM8/24/08
to karrigell
You are aware you can get multiprocess Karrigell by using mod_wsgi?

http://code.google.com/p/modwsgi/wiki/IntegrationWithKarrigell

:-)

Graham

On Aug 24, 1:44 am, Harlin Seritt <harlinser...@yahoo.com> wrote:
> Thanks ccnusjy! Pierre, is this something you're going to include in the Karrigell 3.0?
>
> Thanks,
>
> Harlin Seritt
>

Harlin Seritt

unread,
Aug 24, 2008, 8:01:17 AM8/24/08
to karr...@googlegroups.com
ccnusjy:

I installed processing module and started it up... What's going on here?

Karrigell 2.4.0 running on port 8086
Press Ctrl+C to stop
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python25\Lib\site-packages\processing\forking.py", line 365, in main
    prepare(*preparation_data)
  File "C:\Python25\Lib\site-packages\processing\forking.py", line 329, in prepa
re
  File "<string>", line 1, in <module>
  File "C:\Python25\Lib\site-packages\processing\forking.py", line 365, in main
    prepare(*preparation_data)
  File "<string>", line 1, in <module>
  File "C:\Python25\Lib\site-packages\processing\forking.py", line 365, in main
    prepare(*preparation_data)
    '__parents_main__', file, pathname, etc
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\Karrigell_MP.py", line 6, in <module>
    import KarrigellRequestHandler
  File "C:\Python25\Lib\site-packages\processing\forking.py", line 329, in prepa
re
    '__parents_main__', file, pathname, etc
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\Karrigell_MP.py", line 6, in <module>
    import KarrigellRequestHandler
  File "C:\Python25\Lib\site-packages\processing\forking.py", line 329, in prepa
re
    '__parents_main__', file, pathname, etc
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\Karrigell_MP.py", line 6, in <module>
    import KarrigellRequestHandler
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\KarrigellRequestHandler.py", line 22, in <module>
    import k_config
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\KarrigellRequestHandler.py", line 22, in <module>
    import k_config
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\KarrigellRequestHandler.py", line 22, in <module>
    import k_config
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\core\k_config.py", line 44, in <module>
    _opts, _args = getopt.getopt(sys.argv[1:], "hP:L:SR")
  File "C:\Python25\lib\getopt.py", line 89, in getopt
    opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
  File "C:\Python25\lib\getopt.py", line 153, in do_longs
    has_arg, opt = long_has_args(opt, longopts)
  File "C:\Python25\lib\getopt.py", line 170, in long_has_args
    raise GetoptError('option --%s not recognized' % opt, opt)
getopt.GetoptError: option --processing-fork not recognized
  File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete\Kar
rigell-2.4.0\core\k_config.py", line 44, in <module>
    _opts, _args = getopt.getopt(sys.argv[1:], "hP:L:SR")
  File "C:\Python25\lib\getopt.py", line 89, in getopt
    opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
  File "C:\Python25\lib\getopt.py", line 153, in do_longs
    has_arg, opt = long_has_args(opt, longopts)
  File "C:\Python25\lib\getopt.py", line 170, in long_has_args
      File "C:\Documents and Settings\Harlin\Desktop\Karrigell-2.4.0-dont-delete
\Karrigell-2.4.0\core\k_config.py", line 44, in <module>
    _opts, _args = getopt.getopt(sys.argv[1:], "hP:L:SR")
  File "C:\Python25\lib\getopt.py", line 89, in getopt
    opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
raise GetoptError('option --%s not recognized' % opt, opt)
getopt.GetoptError: option --processing-fork not recognized
  File "C:\Python25\lib\getopt.py", line 153, in do_longs
    has_arg, opt = long_has_args(opt, longopts)
  File "C:\Python25\lib\getopt.py", line 170, in long_has_args
    raise GetoptError('option --%s not recognized' % opt, opt)
getopt.GetoptError: option --processing-fork not recognized

Thanks,

Harlin Seritt


--- On Sat, 8/23/08, ccnusjy <ccn...@gmail.com> wrote:

Zoom.Quiet

unread,
Aug 24, 2008, 8:18:03 AM8/24/08
to karr...@googlegroups.com, Python.cn@google, ZPyUG~珠江三角区Py用户组, 哲思py
this is very very great try!
Collected into woodpecker.org.cn's wiki, sharing and enjoy this enhancement!

http://wiki.woodpecker.org.cn/moin/KarrigellWithPyProcessingVeryGoodStrong

--

http://zoomquiet.org'''
过程改进乃是催生可促生靠谱的人的组织!
PE keeps evolving organizations which promoting people be good!'''

ccnusjy

unread,
Aug 24, 2008, 10:08:44 AM8/24/08
to karrigell
Hello , Seritt:

I forgot to mention the modification of a Karrigell's file "core/
k_config.py".

in line 44:

try:
_opts, _args = getopt.getopt(sys.argv[1:], "hP:L:SR",['processing-
fork'])

#~~~~~~~~~~~~~ here
print _opts,_args
except getopt.GetoptError:
# print usage information and exit:
usage()
raise

I am not familiar with pyprocessing, I found that the child process
forked lost the arguments of their parent process, and they had their
own arguments in sys.argv ('processing-fork' and a process id).


However, it's an ugly way to solve the problem. If some one has better
idea, tell me please.

Zoom.Quiet

unread,
Aug 24, 2008, 10:16:59 AM8/24/08
to karr...@googlegroups.com
On Sun, Aug 24, 2008 at 22:08, ccnusjy <ccn...@gmail.com> wrote:
>
> Hello , Seritt:
>
> I forgot to mention the modification of a Karrigell's file "core/
> k_config.py".
>
that bad ann. but...
i appended the skill in
http://wiki.woodpecker.org.cn/moin/KarrigellWithPyProcessingVeryGoodStrong

so as people said, u need append more doc. for this...

> in line 44:
>
> try:
> _opts, _args = getopt.getopt(sys.argv[1:], "hP:L:SR",['processing-
> fork'])
>
> #~~~~~~~~~~~~~ here
> print _opts,_args
> except getopt.GetoptError:
> # print usage information and exit:
> usage()
> raise
>
> I am not familiar with pyprocessing, I found that the child process
> forked lost the arguments of their parent process, and they had their
> own arguments in sys.argv ('processing-fork' and a process id).
>
>
> However, it's an ugly way to solve the problem. If some one has better
> idea, tell me please.

--

ccnusjy

unread,
Aug 24, 2008, 10:56:56 AM8/24/08
to karrigell
Karrigell is a great web framework ,I like it very much, it's my first
python web framework.

Many thanks to Pierre.

Well, I hope Pierre can provide an offical "Karrgiell_multiprocess.py"
if he agree that it's a good way to improve the performance of
Karrigell.

Maybe, Pierre is busy. I hope more and more people can join the
developement of Karrigell3.0.



On Aug 24, 10:16 pm, Zoom.Quiet <zoom.qu...@gmail.com> wrote:
> On Sun, Aug 24, 2008 at 22:08, ccnusjy <ccnu...@gmail.com> wrote:
>
> > Hello , Seritt:
>
> > I forgot to mention the modification of a Karrigell's file "core/
> > k_config.py".
>
> that bad ann. but...
> i appended the skill inhttp://wiki.woodpecker.org.cn/moin/KarrigellWithPyProcessingVeryGoodS...

Jose C

unread,
Aug 24, 2008, 1:05:22 PM8/24/08
to karrigell
On Aug 23, 11:07 pm, Graham Dumpleton <Graham.Dumple...@gmail.com>
wrote:
> You are aware you can get multiprocess Karrigell by using mod_wsgi?
>
> http://code.google.com/p/modwsgi/wiki/IntegrationWithKarrigell

Hi Graham,

Haven't played with it myself, but I assume this method is attractive
to those not running Karrigell under Apache. I'm not convinced that's
advisable in a web facing environment, but I guess that's another
discussion.

Jose

Pierre Quentel

unread,
Aug 30, 2008, 4:53:41 PM8/30/08
to karrigell
Hi,

Great idea, thanks very much. I have adapted it for version 3.0, you
can find it in the current tarball

I have just fixed the code for k_config, the option must be written
"processing-fork=" with a = at the end to indicate that there is an
argument

Thanks again
Pierre

Harlin Seritt

unread,
Sep 21, 2008, 10:14:25 AM9/21/08
to karr...@googlegroups.com
I've put this in place on a windows box and it works but for some reason it doesn't work on a Linux server. When I start it up it starts with no errors and appears to run. Yet I can't connect to it via the web browser. Any ideas?


Thanks,

Harlin Seritt

--- On Sat, 8/23/08, ccnusjy <ccn...@gmail.com> wrote:
From: ccnusjy <ccn...@gmail.com>
Subject: [karrigell] Karrigell + PyProcessing,Very good and Strong!
To: "karrigell" <karr...@googlegroups.com>
Date: Saturday, August 23, 2008, 6:14 AM

ccnusjy

unread,
Sep 21, 2008, 10:44:07 AM9/21/08
to karrigell
Hi Harlin,

I have tested Karrigell_multprocess.py on Ubuntu7.x/Freebsd6.2,and it
works very welll for me.

I have no idea about what happened on your linux system.

Would you please have a look at 'ps all | grep Karrigell' to make sure
that it runs in multi-process mode.

Another question, what's your Karrigell version? please use
Karrigell2.4 because the Karrigell3.0 still has some little bugs.


Wish you good luck.

Junyi Sun





On Sep 21, 10:14 pm, Harlin Seritt <harlinser...@yahoo.com> wrote:
> I've put this in place on a windows box and it works but for some reason it doesn't work on a Linux server. When I start it up it starts with no errors and appears to run. Yet I can't connect to it via the web browser. Any ideas?
>
> Thanks,
>
> Harlin Seritt
>
> --- On Sat, 8/23/08, ccnusjy <ccnu...@gmail.com> wrote:
> From: ccnusjy <ccnu...@gmail.com>

Harlin Seritt

unread,
Sep 21, 2008, 11:27:34 AM9/21/08
to karr...@googlegroups.com
I get this...

[root@vlin-ila-wik01 Karrigell-2.4.0-with_mp]# ps all | grep -i karri
0     0 21348 21240  18   0  11596  6004 -      S+   pts/1      0:00 python Karrigell_multiprocess.py
1     0 21351 21348  18   0  11596  4132 -      S+   pts/1      0:00 python Karrigell_multiprocess.py
1     0 21352 21348  18   0  11596  4132 -      S+   pts/1      0:00 python Karrigell_multiprocess.py
1     0 21353 21348  18   0  11596  4132 -      S+   pts/1      0:00 python Karrigell_multiprocess.py

And still not able to connect to it via browser ... I test that out also with urllib2:

>>> import urllib2
>>> url = 'http://9.9.149.167:8082'
>>> data = urllib2.urlopen(url).read()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Python25\lib\urllib2.py", line 121, in urlopen
    return _opener.open(url, data)
  File "c:\Python25\lib\urllib2.py", line 374, in open
    response = self._open(req, data)
  File "c:\Python25\lib\urllib2.py", line 392, in _open
    '_open', req)
  File "c:\Python25\lib\urllib2.py", line 353, in _call_chain
    result = func(*args)
  File "c:\Python25\lib\urllib2.py", line 1100, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "c:\Python25\lib\urllib2.py", line 1075, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error (10061, 'Connection refused')>

and then netstat -an:

[root@vlin-ila-wik01 Karrigell-2.4.0-with_mp]# netstat -an | grep 8082
tcp        0      0 127.0.0.1:8082              0.0.0.0:*                   LISTEN

So I know it's listening but it's not accepting or something... any ideas? Is it possible you can send me a copy of your multiprocess file and k_config.py you're using?

Thanks,

Harlin Seritt



--- On Sun, 9/21/08, ccnusjy <ccn...@gmail.com> wrote:

Harlin Seritt

unread,
Sep 21, 2008, 11:34:04 AM9/21/08
to karr...@googlegroups.com
Also, which version of Python are you using? I am wondering if that may be an issue with the processing module.

Thanks,

Harlin Seritt

--- On Sun, 9/21/08, ccnusjy <ccn...@gmail.com> wrote:
From: ccnusjy <ccn...@gmail.com>
Subject: [karrigell] Re: Karrigell + PyProcessing,Very good and Strong!
To: "karrigell" <karr...@googlegroups.com>
Date: Sunday, September 21, 2008, 10:44 AM

junyi sun

unread,
Sep 21, 2008, 9:47:07 PM9/21/08
to karr...@googlegroups.com
Hi Harlin, I use python2.5.2。 I think I know what's your problem. I found that the code in my post above "s = Server.Server(('localhost',port),asyncRequestHandler)" specify that the server can only listen at localhost or 127.0.0.1, so connection to 9.9.149.167 was refused. To solve the problem , please modify the code like this "s = Server.Server(('',port),asyncRequestHandler)" to make the server listen at all the IP addresses of your machine. I am sorry to that. I made the configuration because I used the Karrigell behind a Apache server by mod_proxy, so I didn't want to expose other ports to Internet except 80. To reduce your trouble, I upload my code of Karrigell_multiprocess.py and k_config.py in the attachments;and I wish these would be helpful. Junyi Sun


2008/9/21 Harlin Seritt <harlin...@yahoo.com>
Karrigell_multiprocess.py
k_config.py

Harlin Seritt

unread,
Sep 22, 2008, 5:46:27 AM9/22/08
to karr...@googlegroups.com
Junyi! Thanks for the help! I will try to implement this today. This is for a few apps I run at work that does involve an rsync call and thereby incurs occasionally long-running threads... thus the need for multi-processing. Thanks for pointing this out. I will let you know if I run into any snags.

Best Regards,

Harlin

--- On Sun, 9/21/08, junyi sun <ccn...@gmail.com> wrote:
#! /usr/bin/python

"""Karrigell HTTP Server

Written by Pierre Quentel quentel...@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 an asynchronous server (uses non-blocking sockets and the select()
function)

Requests are handled by class RequestHandler (one instance per request)
def main_start(process_num):
# Launch the server
port = k_config.port

s = Server.Server(('',port),asyncRequestHandler)


print "Karrigell %s running on port %s"
%(KarrigellRequestHandler.__version__,
port)
print "Press Ctrl+C to stop"

for i in range(process_num):
child = Process(target=kkk_serve, args=(s,))
child.setDaemon(True)
child.start()

kkk_serve(s)

if __name__ == '__main__':
if sys.platform == 'win32':
freezeSupport()
    main_start(process_num=3)

# reads the Configuration File
# the variable 'base' is set to current directory
import ConfigParser
import os
import sys
import getopt
import traceback
import urllib
import re

# set default values
port = 80 # the standard HTTP port, can be set in command line
reload_modules = True
debug = 1
show_trace = True
silent = False
thread_safe = False
gzip = False
persistentSession = 0
base = '' # base URL (used for Xitami)
ignore = []
allow_directory_listing = False
hide_extensions = []
hide_paths = []
language=""
output_encoding = 'iso-8859-1'

encode_form_data = False
loggingFile = ''
loggingParameters = '"a",0,10'

class ConfigError(Exception):
pass

def usage():
print "Usage : python Karrigell.py [-P port] [-S] [-H]
[initFile]"
print "\n\tport = HTTP port"
print "\n\tS = silent (no output written to console)"
print "\n\tH = help (shows this message)"
print "\n\tinitFile = the initialization file to be used"

# get the command-line options (parsed later, after the configuration file)

try:
_opts, _args = getopt.getopt(sys.argv[1:],
"hP:L:SR",['processing-fork='])

print _opts,_args
except getopt.GetoptError:
# print usage information and exit:
usage()
raise

# server directory
if len (sys.argv) > 0:
serverDir=os.path.dirname(os.path.abspath(sys.argv[0]))
else:
serverDir=os.getcwd()

# default configuration file
initFile = os.path.join(serverDir,"conf","Karrigell.ini")

if _args:
if len(_args) == 1:
initFile = _args[0]
else:
raise ValueError,"No argument for init file, args = %s"
%str(_args)
#usage()
#sys.exit(2)

for o, a in _opts:
if o in ("-h", "--help"):
usage()
sys.exit()
if o in ("-S", "--silent"):
silent=1
if o in ("-P", "--port"):
try:
port=int(a)
except ValueError:
raise ConfigError, _("Error - port must be an integer")

conf=ConfigParser.ConfigParser({'base':serverDir})
try:
conf.read(initFile)
except ConfigParser.ParsingError,message:
parsingErrorMsg=_("Parsing error")
print parsingErrorMsg,message
print type(message)
print message.__dict__
for item in message._args:
print item[1]
sys.exit(2)

# Server
try:
port=int(conf.get("Server","port"))
except ValueError:
raise ConfigError, _("Error - port must be an integer")
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass
except:
traceback.print_exc(file=sys.stderr)

# reload_modules level
# 1 = reloads imported files every time
try:
reload_modules =
conf.getboolean("Server","reload_modules")
except:
pass

# debug level
# 1 = show the "Debug" button on error pages
try:
debug = conf.getint("Server","debug")
except:
pass

# show trace on error / exception ?
try:
show_trace = conf.getboolean("Server","show_trace")
except:
pass

# silent mode
try:
silent=conf.getboolean("Server","silent")
except:
pass

# thread safety
try:
thread_safe=conf.getboolean("Server","thread_safe")
except:
pass

# gzip support
try:
gzip=conf.getboolean("Server","gzip")
except:
pass

# persistent sessions
persistentSession = False
try:

persistentSession=conf.getboolean("Server","persistentSession")
except:
pass

# urls to ignore
try:
ignore=conf.get("Server","ignore").split(';')
except:
pass

# user-defined values
globalScripts=[]
try:

_globalScriptsList=conf.get("Server","global").split(";")
for _globalScript in _globalScriptsList:

_gPath,_gName=os.path.dirname(_globalScript),os.path.basename(_globalScript)
_gPath = os.path.normpath(_gPath)
if not _gPath in sys.path:
sys.path.append(_gPath)
_gName=os.path.splitext(_gName)[0]
exec ('import '+os.path.basename(_gName))
globalScripts.append(_gName)
except (ConfigParser.NoSectionError,ConfigParser.NoOptionError):
pass

# output encoding
try:
output_encoding=conf.get("Server","output_encoding")
except (ConfigParser.NoSectionError,ConfigParser.NoOptionError):
pass

try:
unicode('a',output_encoding)
except LookupError:
print "Unknown output_encoding in configuration file : %s"
%output_encoding
sys.exit()

# form data encoding
try:
encode_form_data =
conf.getboolean("Server","encode_form_data")
except (ConfigParser.NoSectionError,ConfigParser.NoOptionError):
pass

# Root Directory, default to server directory
rootDir=os.path.join(serverDir,"webapps")
try:
rootDir=conf.get("Directories","root")
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass
except:
traceback.print_exc(sys.stderr)

if not os.path.isdir(rootDir):
raise ConfigError, "root directory not found : %s" %rootDir

# cgi Directory, default to root/cgi-bin
cgi_directories = [os.path.join(rootDir,"cgi-bin")]
try:

cgi_directories=conf.get("Directories","cgi").split(';')
cgi_directories = [os.path.normpath(d) for d in cgi_directories ]
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass
except:
traceback.print_exc(sys.stderr)

# protected zones : for all scripts, restrict access to admin
protectedDirs={}
try:
protectedDirs =
conf.get("Directories","protected").split(";")
protectedDirs = dict([ (os.path.normpath(_p),"admin") for _p in
protectedDirs ])
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass
except:
traceback.print_exc(sys.stderr)
sys.exit()

# files with extension in hide_extensions won't be shown
try:
hide_extensions =
conf.get("Directories","hide_extensions")
hide_extensions = [ _h.strip() for _h in
hide_extensions.split(";") ]
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass

# files matching the patterns in hide_paths won't be shown
try:
hide_paths = conf.get("Directories","hide_paths")
hide_paths = [ re.compile(_h.strip()) for _h in
hide_paths.split(";") ]
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass

# logging directory
try:
loggingFile=conf.get("Directories","loggingFile")
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass

# logging parameters
try:

loggingParameters=conf.get("Directories","loggingParameters")
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass


# Applications
extensions_map={}
try:
extensions=conf.options("Applications")
for _extension in extensions:

extensions_map["."+_extension]=conf.get("Applications",_extension)
except ConfigParser.NoSectionError:
pass

# Aliases
alias={}
try:
_aliases=conf.options("Alias")
for _al in _aliases:
_path=conf.get("Alias",_al)
alias[_al]=os.path.normpath(urllib.unquote(_path))
if not 'base' in _al:
del alias['base']
except ConfigParser.NoSectionError:
pass

# default language (added by Sylvain Ramousse)
try:
language=conf.get("Translation","lang")
except (ConfigParser.NoSectionError,ConfigParser.NoOptionError):
pass

# virtual hosts
virtual_hosts = {}

# default
virtual_hosts[0] = {
'silent' : silent,
'thread_safe' : thread_safe,
'reload_modules' : reload_modules,
'port' : port,
'gzip' : gzip,
'persistentSession' : persistentSession,
'ignore': ignore,
'globalScripts' : globalScripts,
'rootDir' : rootDir,
'protectedDirs' : protectedDirs,
'allow_directory_listing' : allow_directory_listing,
'hide_extensions' : hide_extensions,
'extensions_map' : extensions_map,
'alias' : alias,
'language' : language
}

def get_allow_dir_list(adl,host):
if not isinstance(adl,bool):
raise ConfigError, "Error in configuration file %s : " \
"allow_directory_listing = %s for virtual host %s"\
'(must be 0 or 1)' %(initFile,adl,
host)
return adl

# determine who can read directory listings
try:
allow_directory_listing = conf.getboolean("Directories",
"allow_directory_listing")
except (ConfigParser.NoOptionError,ConfigParser.NoSectionError):
pass

virtual_hosts[0]['allow_directory_listing'] = \
get_allow_dir_list(allow_directory_listing,0)

for section in conf.sections():
if section.strip().lower().startswith('virtualhost'):
host = section.split()[1]
virtual_hosts[host] = {}
for option in conf.options(section):
if option == 'root':
if os.path.isdir(conf.get(section,option)):
virtual_hosts[host]['rootDir'] =
conf.get(section,option)
else:
raise IOError,'Root directory for virtual host %s'
\
' not found : %s' %(host,conf.get(section,option))
elif option == 'allow_directory_listing':
adl = conf.get(section,option)
virtual_hosts[host]['allow_directory_listing'] = \
get_allow_dir_list(adl,host)

# import modules to handle scripts
handled_extensions = []
for path in ["core","modules","debugger"]:
p = os.path.join(serverDir,path)
if not p in sys.path:
sys.path.append(p)
for f in
os.listdir(os.path.join(serverDir,"core","modules")):
if f.startswith('mod_') and f.endswith('.py'):
module_name = os.path.splitext(f)[0]
handled_extensions.append(module_name[4:].lower())

if __name__=="__main__":
for k,v in globals().items():
print "%s : %s" %(k,v)

Harlin Seritt

unread,
Sep 22, 2008, 6:43:22 PM9/22/08
to karr...@googlegroups.com
I did some testing with this and found some bleed-over from one session to another. Since many different users will be using this and possibly at one time, I have to think that this is not going to work. Is there anything else that can be done to keep sessions from running into each other?

thanks,


Harlin

--- On Sun, 9/21/08, junyi sun <ccn...@gmail.com> wrote:
#! /usr/bin/python

"""Karrigell HTTP Server

Written by Pierre Quentel quentel...@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 an asynchronous server (uses non-blocking sockets and the select()
function)

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

def main_start(process_num):
# Launch the server
port = k_config.port

s = Server.Server(('',port),asyncRequestHandler)


print "Karrigell %s running on port %s"
%(KarrigellRequestHandler.__version__,
port)
print "Press Ctrl+C to stop"

for i in range(process_num):
child = Process(target=kkk_serve, args=(s,))
child.setDaemon(True)
child.start()

kkk_serve(s)

if __name__ == '__main__':
if sys.platform == 'win32':
freezeSupport()
    main_start(process_num=3)

junyi sun

unread,
Sep 23, 2008, 9:52:03 PM9/23/08
to karr...@googlegroups.com
Hi Harlin:

    Session Sharing between processes is realy a problem. I wrote some code which implemented session on memcached.

#memcache_session.py
#author:sunjoy
#module:session based on memcached
#date:2008-8-8
import uuid
import cmemcache

SESSION_FLAG = "zsessionId"
GLOBAL_FLAG = "global.vars."
MC_SERVER = ["127.0.0.1:11211"]

def getMC():
    mc = cmemcache.Client(MC_SERVER, debug=0)
    return mc

def get_session_id(zCOOKIE,SET_zCOOKIE):
    session_id = zCOOKIE.get(SESSION_FLAG,None)
    if not session_id: session_id = SET_zCOOKIE.get(SESSION_FLAG,None)

    if session_id:
        session_id = session_id.value
        return session_id
    else:
        session_id = str(uuid.uuid1())
        SET_zCOOKIE[SESSION_FLAG] = session_id
        return session_id

def get_session_var(k,zCOOKIE,SET_zCOOKIE):
    mc = getMC()
    session_id = get_session_id(zCOOKIE,SET_zCOOKIE)
    result = mc.get(session_id+"_"+k)
    return result

def set_session_var(k,v,zCOOKIE,SET_zCOOKIE):
    mc = getMC()
    session_id = get_session_id(zCOOKIE,SET_zCOOKIE)
    mc.set(session_id+"_"+k,v)


def get_global_var(k):
    mc = getMC()
    result =  mc.get(GLOBAL_FLAG+k)
    return result


def set_global_var(k,v):
    mc = getMC()
    mc.set(GLOBAL_FLAG+k,v)

def del_global_var(k):
    mc = getMC()
    mc.delete(GLOBAL_FLAG+k)


USAGE:

a.py
===========
from memcache_session import *
set_session_var("user_id","foobar",COOKIE,SET_COOKIE)

b.py
===========
from memcache_session import *
user_id = get_session_var("user_id",COOKIE,SET_COOKIE)
print user_id


A little ugly interface.

Any good idea?



Reply all
Reply to author
Forward
0 new messages