page contents are not refreshed

G

Gleb Rybkin

when running apache, mod_python in windows.

This looks pretty strange. Creating a simple python file that shows
current time will correctly display the time in apache the first time,
but freezes afterwards and shows the same time on all subsequent clicks
as long as the file is not modified.

Any ideas what's wrong? Thanks.

from mod_python import apache
from time import strftime, gmtime

curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
def handler(req):
req.content_type = "text/plain"
req.send_http_header()
req.write(str(curtime))
return apache.OK
 
S

Steve Holden

Gleb said:
when running apache, mod_python in windows.

This looks pretty strange. Creating a simple python file that shows
current time will correctly display the time in apache the first time,
but freezes afterwards and shows the same time on all subsequent clicks
as long as the file is not modified.

Any ideas what's wrong? Thanks.

from mod_python import apache
from time import strftime, gmtime

curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
def handler(req):
req.content_type = "text/plain"
req.send_http_header()
req.write(str(curtime))
return apache.OK
Try moving the assignment to curtime inside the handler function so it
isn't just executed once when the module is imported ...

regards
Steve
 
W

waylan

Gleb said:
when running apache, mod_python in windows.

This looks pretty strange. Creating a simple python file that shows
current time will correctly display the time in apache the first time,
but freezes afterwards and shows the same time on all subsequent clicks
as long as the file is not modified.

Any ideas what's wrong? Thanks.

The first time the page was requested mod_python compiled and loaded
your code. Every request after that mod_python refers to the already
loaded code in memory in which your expression had already been
evaluated the first time.

Therefore, you need to make curtime a 'callable object' so that it will
be re-evaluated on each request. Unfortunelty, I don't recall if simply
wraping your strftime() expression in a function will be enough or if
its more complex that that. That said, I **think** this should work:
from mod_python import apache
from time import strftime, gmtime
def curtime():
return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
 
S

Steve Holden

waylan said:
The first time the page was requested mod_python compiled and loaded
your code. Every request after that mod_python refers to the already
loaded code in memory in which your expression had already been
evaluated the first time.

Therefore, you need to make curtime a 'callable object' so that it will
be re-evaluated on each request. Unfortunelty, I don't recall if simply
wraping your strftime() expression in a function will be enough or if
its more complex that that. That said, I **think** this should work:


def curtime():
return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
This is a very long way round for a shortcut (though it does have the
merit of working). Why not just

def handler(req):
req.content_type = "text/plain"
req.send_http_header()
curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
req.write(str(curtime))
return apache.OK

Or even

def handler(req):
req.content_type = "text/plain"
req.send_http_header()
req.write(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
return apache.OK

regards
Steve
 
T

tobiah

Browser Cache?

Gleb said:
when running apache, mod_python in windows.

This looks pretty strange. Creating a simple python file that shows
current time will correctly display the time in apache the first time,
but freezes afterwards and shows the same time on all subsequent clicks
as long as the file is not modified.

Any ideas what's wrong? Thanks.

from mod_python import apache
from time import strftime, gmtime

curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
def handler(req):
req.content_type = "text/plain"
req.send_http_header()
req.write(str(curtime))
return apache.OK
 
W

waylan

Steve said:
waylan wrote: [snip]
def curtime():
return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
This is a very long way round for a shortcut (though it does have the
merit of working). Why not just

def handler(req):
req.content_type = "text/plain"
req.send_http_header()
curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
req.write(str(curtime))
return apache.OK

Or even

def handler(req):
req.content_type = "text/plain"
req.send_http_header()
req.write(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
return apache.OK

While Steve's examples certainly do the trick in this limited case, I
assumed that the original poster was just starting with mod_python and
I was simply trying to explain the bigger picture for future reference.
As one develops more sophisticated code, simply adding it to the
`handler` function becomes less desirable. Reacognizing that anything
that must be reevaluated on each request must be callable will be a
bigger help IMHO.

Steve's examples work because the current time is evaluated within
`handler` and :
True

While in the the original example:
False

Yet in my example:
True

Finally, by way of explaination:
'callable(object) -> bool\n\nReturn whether the object is callable
(i.e., some kind of function).\nNote that classes are callable, as are
instances with a __call__() method.'
 
B

Bruno Desthuilliers

waylan a écrit :
(snip)
While Steve's examples certainly do the trick in this limited case, I
assumed that the original poster was just starting with mod_python and
I was simply trying to explain the bigger picture for future reference.
As one develops more sophisticated code, simply adding it to the
`handler` function becomes less desirable.

Indeed, but...
Reacognizing that anything
that must be reevaluated on each request must be callable

There it gets plain wrong.

(snip)
Steve's examples work because the current time is evaluated within
`handler` and :



True

While in the the original example:



False

The fact that the object bound to curtime being callable or not doesn't
change anything to the problem. You can write as many functions as you
want outside the handler, if they don't get called by the handler, this
won't be of no use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top