7

I couldn't find out python equivalent to PHP $_SERVER.

Is there any? Or, what are the methods to bring equivalent results?

Thanks in advance.

fireball003
  • 1,909
  • 5
  • 20
  • 24
  • 3
    There's a lot of differences between Python and PHP. Python is just a programming language, whereas PHP is more like a web framework, as it has primitives for web related stuff. Like $_SERVER, header, setcookie. These are things that are not in Python's core, but rather in domain-specific modules. PHP was built to be a language of the web, that's why it has so many useful things for web programming. – Ionuț G. Stan Jun 23 '09 at 08:57
  • 1
    Anyway, PHP is idiosyncratic, as you may access the $_SERVER superglobal even in CLI mode. It gets populated in this scenario too. – Ionuț G. Stan Jun 23 '09 at 08:58
  • 1
    Well... Things are changing. Python is making its way. So, it's worth it to learn and build some sites using python. – fireball003 Jun 23 '09 at 12:11

2 Answers2

11

Using mod_wsgi, which I would recommend over mod_python (long story but trust me) ... Your application is passed an environment variable such as:

def application(environ, start_response):
    ...

And the environment contains typical elements from $_SERVER in PHP

...
environ['REQUEST_URI'];
...

And so on.

http://www.modwsgi.org/

Good Luck

REVISION The real correct answer is use something like Flask

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
1

You don't state it explicitly, but I assume you are using mod_python? If so, (and if you don't want to use mod_wsgi instead as suggested earlier) take a look at the documentation for the request object. It contains most of the attributes you'd find in $_SERVER.
An example, to get the full URI of the request, you'd do this:

def yourHandler(req):
    querystring=req.parsed_uri[apache.URI_QUERY]

The querystring attribute will now contain the request's querystring, that is, the part after the '?'. (So, for http://www.example.com/index?this=test, querystring would be this=test)

carlpett
  • 12,203
  • 5
  • 48
  • 82