Code Point
September 10, 2010, 06:04:15 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Geshi Sintax Hilighting Mod Installed. Tens of languages available.
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Viewing the raw request data  (Read 804 times)
ralienpp
Jr. Member
*

Karma: 0
Offline Offline

Posts: 4


WWW
« on: May 07, 2010, 01:14:55 PM »

Hi everyone,

I have a question which may sound trivial, but unfortunately I found no answer.

How can I view the raw data of a GET or a POST request?

I have this url: http://127.0.0.1/element.py?data=asdasdasd

And the following code
Code
#!/usr/bin/python
from mod_python import apache
from mod_python import util
 
def index(req):
a = req.read()
return """<html><body>foo %s bar </body></html>""" % (a)
 

When I view the page, I see "foo bar", although I expected some content to be present in the a variable.

Can someone tell me how I can view the raw data of a request?  Alternatively, if there is a way to view the non-raw data (i.e. maybe mod_python automagically parsed it for me), perhaps I should use that way instead?

Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #1 on: May 07, 2010, 01:38:21 PM »

This page shows most of methods and attributes of the request object:

http://webpython.codepoint.net/files/other/request.html

If you want the query string then it is here:
Code
a = req.subprocess_env['QUERY_STRING']
 
Logged
ralienpp
Jr. Member
*

Karma: 0
Offline Offline

Posts: 4


WWW
« Reply #2 on: May 07, 2010, 01:51:08 PM »

Thank you for the swift response, this is exactly what I was looking for.

I want to add that I was tinkering with the code and printed req itself - I saw something like <mp_request object at 0xblabla>.  I searched the net for "mp_request" and found this page: http://modpython.org/live/current/doc-html/pyapi-mprequest.html, there is a list of functions and attributes of the class.


I believe it was really stupid of me to miss that resource :-)
Logged
ralienpp
Jr. Member
*

Karma: 0
Offline Offline

Posts: 4


WWW
« Reply #3 on: May 07, 2010, 07:36:29 PM »

There is an issue which I cannot explain - the method described earlier works fine for GET requests, but the POST requests are always empty.

My understanding is that in the case of POST, the data are passed to the STDIN, so I need to read from this source.

The script:
Code:
#!/usr/bin/python
from mod_python import apache
from mod_python import util
import sys

def index(req):
raw = sys.stdin.read(5)
return """<html><body>foo bar %s </body></html>""" % (raw)


I use this to test it
Code:
import httplib, urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
print params
conn = httplib.HTTPConnection(MYHOST)
conn.request("POST", MYURL, params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()


The server responds with 200 OK, but raw remains empty, even though I was expecting to see 5 bytes read from STDIN.

Is this improper use of the functions? Or can it be explained by an issue with the settings in httpd.conf?
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #4 on: May 07, 2010, 09:26:32 PM »

Did you try
Code
req.read()
?
« Last Edit: May 07, 2010, 09:28:53 PM by Alter Lobo » Logged
ralienpp
Jr. Member
*

Karma: 0
Offline Offline

Posts: 4


WWW
« Reply #5 on: May 07, 2010, 10:17:54 PM »

Yes, tried that - it is empty.

If I understand you correctly, the script in my first message should work.
Logged
Alter Lobo
Global Moderator
Jr. Member
*****

Karma: 10
Offline Offline

Posts: 65


« Reply #6 on: May 07, 2010, 10:25:04 PM »

If you are using the publisher the POST data will always be consumed as it always parses the form content. If you want the raw POST response then you can't use the publisher. If you want it parsed use req.form as show here:

http://webpython.codepoint.net/mod_python_publisher_form
« Last Edit: May 07, 2010, 10:26:51 PM by Alter Lobo » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!