Jamie
Jamie

Reputation: 115

Get current, requested URL in Python without a framework

I'm trying to get the URL that has been requested in Python without using a web framework.

For example, on a page (let's say /main/index.html), the user clicks on a URL to go to /main/foo/bar (/foo/bar doesn't exist). Apache (with mod_wsgi) then redirects the user to a PHP script at /main/, which then gets the url and searches MySQL for any matching fields. Then the rest of the field is returned. This helped in PHP:

$_SERVER["REQUEST_URI"];

I'd rather not use PHP since it's becoming increasingly difficult to maintain the PHP code whilst the database keeps changing in structure.

I'm pretty sure there's a better way altogether and any mention would be greatly appreciated. For the sake of relevancy, is this even possible (to get the requested URL in Python)? Should I just use a framework, although it seems quite simple?

Thanks in advance,

Jamie

Note: I don't want to use GET for security purposes.

Upvotes: 1

Views: 8157

Answers (4)

p1l0t
p1l0t

Reputation: 115

When I want to get a URL outside of any framework using Apache2 and Mod_WSGI I use

environ.get('PATH_INFO')

inside of my application() function.

Upvotes: 1

Well, if you run your program as a CGI script, you can get the same information in os.environ. However, if I recall correctly, REQUEST_URI as such is not part of the CGI standard and you need to use os.environ['SCRIPT_NAME'], os.environ['PATH_INFO'] and os.environ['QUERY_STRING'] to get the equivalent data.

However, I seriously urge you to see some lightweight framework, such as Pyramid. Plain CGI with Python is slow and generally just pain in the ass.

Upvotes: 9

Bite code
Bite code

Reputation: 596723

Unlike PHP, Python is a general purpose language and doesn't have this built-in.

The way you can gather this information depends on the deployment solution:

  • CGI (mostly Apache with mod_python, deprecated): see @Antti Haapala solution
  • WSGI (most other deployment solutions): see @gurney alex solution

But you will encouter much more problems: session hanling, url management, cookies, and even juste simple POST/GET parsing. All of this need to be done manually if you don't use a framework.

Now, if you feel like a framework is overkill (but really, incredible tools like Django are worth it), you can use a micro framework like bottle.

Microframeworks will typically make this heavy lifting for you, but without the complicated setup or the additional advanced features. Bottle has actually zero setup an is a one file lib.

Hello word with bottle:

from bottle import route, run, request

@route('/hello/:name')
def index(name='World'):
    return '<b>Hello %s! You are at %s</b>' % (name, request.path)

run(host='localhost', port=8080)

request.path contains what you want, and if you visit http://127.0.0.1:8080/hello/you, you will get:

Hello you! You are at /hello/you

Upvotes: 1

gurney alex
gurney alex

Reputation: 13645

When using mod_python, if I recall correctly you can use something like:

from mod_python import util
def handler(request):    
    parameters = util.FieldStorage(request)
    url = parameters.get("url", "/")

See http://www.modpython.org/live/current/doc-html/pyapi-util.html for more info on the mod_python.util module and the FieldStorage class (including examples)

Upvotes: 0

Related Questions