views:

286

answers:

2

So, within a webapp.RequestHandler subclass I would use self.request.uri to get the request URI. But, I can't access this outside of a RequestHandler and so no go. Any ideas?

I'm running Python and I'm new at it as well as GAE.

+2  A: 

You should generally be doing everything within some sort of RequestHandler or the equivalent in your non-WebApp framework. However, if you really insist on being stuck in the early 1990s and writing plain CGI scripts, the environment variables SERVER_NAME and PATH_INFO may be what you want; see a CGI reference for more info.

Wooble
Yes, first import os and then look into `os.environ['PATH_INFO']` and `os.environ['QUERY_STRING']`.
jhs
I would disagree that it's just a 90s thing. Different apps have a variety of needs. Once I had to ensure a trailing slash in URLs. Instead of webapp, I just checked `PATH_INFO` and set the `Location` header if it was wrong. Of course in general I agree with Woobie that you need a framework.
jhs
The reason I'm looking to do this is to avoid passing this value through a long string of functions. Maybe I need to rethink how I'm writing this?
donut
A: 

Since using request outside code handling it is meaningless I assume you'd like to access it from some method called by handler without passing request to it. Your choices are:

  1. Refactor code so that request is passed to it.
  2. When the former is not possible use a hack by defining a global threading.local(), storing request somewhere in request handler and access it in your method.
Denis Otkidach
You don't need threading.local() because App Engine Python is single-threaded.
Nick Johnson
Haha, you've got me pegged. Look at my comment on Wooble's answer. I'm thinking I'll need to look at re-factoring my code.
donut