Client-server data exchange: handling requests

Using web application on the HTTP protocol consists in a serie of requests and responses: viewing an HTML page on a web browser is a communication that involves a client (the web browser on the customer’s computer) and a server (like Apache on your web server). It all starts with a “request” message generated from the client and one (or more) “response” message(s) sent back by the server to the user’s browser, containing the desired page.

Using PyHP to manage dynamic web pages means controlling those requests to build the desired page and sending it back through a response. In order to do that, PyHP offers you different special objects, referring to the different types of data communication supported by the HTTP+(x)HTML standard: using those objects you will be able to control HTTP request headers, response headers, GET data requests, POST data requests and cookies. For the first four ones you can continue reading this manual section; for cookies, please refer to the appropriate CookieManagement page.

Note

for further information about the HTTP communication process, please refer to the available standars RFCs.

Note

obviously, PyHP supports other “de-facto standards” for web-related data manipulation and handling, such as file uploads, database connections and sessions. There are specific sections of this manual available for this features: see the “Documentation summary” paragraph in the PyhpUse page.

Reading request headers, setting response headers

HTTP headers are special data fields containing information attached to HTTP messages (requests and responses), used to set specific settings or data parsed by the web server and/or the browser. Referring to the HTTP/1.1 RFC 2616, there are several headers types: Accept, Accept-Charset, Accept-Encoding, Accept-Language, Accept-Ranges, Age, Allow, Authorization, Cache-Control, Connection, Content-Encoding, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Type, Date, ETag, Expect, Expires, From, Host, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Last-Modified, Location, Max-Forwards, Pragma, Proxy-Authenticate, Proxy-Authorization, Range, Referer, Retry-After, Server, TE, Trailer, Transfer-Encoding, Upgrade, User-Agent, Vary, Via, Warning and WWW-Authenticate. For a complete reference about this headers, see the official RFC page at http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Most of them are managed by the browser and/or the web server: anyway, it might be useful to control some of them in our web applications. You can easily read and modify them using two PyHP special objects:

pyhp.headers_in
pyhp.headers_out

These are Python dictionaries, so you can get and set data from and to them using the standard Python operations for dictionaries. Syntax examples:

received_value = pyhp.headers_in['Header-Param-Name']
pyhp.headers_out['Header-Param-Name'] = 'value'

Managing GET and POST methods

HTTP GET and POST methods are two ways defined by the HTTP standard to pass data from the user’s browser to the web server, sending values within the request. The main difference is where data are put on the request: while a GET method will send data encoding it into the URI field, the POST method encodes it into the HTTP message body.

Note

as suggested above, for further information on the HTTP protocol and how GET and POST methods work in the standard, see the HTTP reference documentation.

As for headers, GET and POST data are stored into Python dictionaries exposed by the PyHP object:

pyhp.get
pyhp.post

and you can get/set data as for any other Python dictionary:

received_value = pyhp.get['field_name']
received_value = pyhp.post['field_name']

GET and POST field arrays can be filled for example by a specific-formed anchor link or HTML form, such as:

<a href="myModule.pyhp?myField=myValue">My link</a>


<form action="myModule.pyhp" method="post">
  <input type="text" name="myField" value="thisFieldDefaultValue" />
</form>

Response status code

HTTP protocol defines different status code given with the response message to user’s browser, in order to make it understand if there was an error or the resource is not available or if everything went OK with the server request.

This status code is managed and sent by Apache for standard HTML/XHTML web pages. The PyHP preprocessor is called if the page really exists and it’s being processed by Apache, so since the PyHP preprocessor always leave the execution in a coherent state you’ll always get the same status code as if the page were a standard HTML page. This means that if you request a real page that exists, it’s reachable and it’s built with a correct sintax, you’ll always receive an ‘OK 200’ response status code, even if your PyHP code generates exeptions: managing inconsistent states of your Python code logic is left to your own script expection handling management.