0

I am trying to understand http queries and was succesfully getting the data from GET requests through the environment variables by first looking through the keys of the environment vars and then accessing 'QUERY_STRING' to get the actual data.

like this:

#!/usr/bin/python3
import sys
import cgi
import os

inputVars = cgi.FieldStorage()

f = open('test','w')
f.write(str(os.environ['QUERY_STRING])+"\n")
f.close()

Is there a way to get the POST data (the equivalent of 'QUERY_STRING' for POST - so to say) as well or is it not accessible because the POST data is send in its own package? the keys of the environment variables did not give me any hint so far.

gaugau
  • 765
  • 1
  • 9
  • 30
  • 2
    Possible duplicate of [How are POST and GET variables handled in Python?](http://stackoverflow.com/questions/464040/how-are-post-and-get-variables-handled-in-python) – syntonym Jun 28 '16 at 12:13
  • thanks for the suggestion! sadly its not what i tried to ask. i am looking for the POST equivalent von 'QUERY_STRING' in a way. I do understand how to get single parameters through cgi FieldStorage but I try to get the Post message data in its original formatting. I hope that clarifies my question. i edited my question a bit. – gaugau Jun 28 '16 at 12:25
  • 1
    The second answer from Schien says: `n Python, these functions would be os.getenv('QUERY_STRING') and sys.stdin.read(). Remember to import os and sys modules.` so I think the equivalent would be to read on stdin. There should probably be a cgi standard that defines that? – syntonym Jun 28 '16 at 12:41
  • you are absolutely right. i should read the comments more deeply! they most of the time always deliver these awesome additional answers. thanks for pointing that out! im quite new here. is it useful to keep duplicates online? or should i delete my question? – gaugau Jun 28 '16 at 12:44
  • 1
    I guess your question isn't really a duplicate of the question, because you are not asking for generally handling but specifically about environment variables. So I guess your question is valuable because if someone else asks himself that specific question (s)he will probably find your question and not the other one (even if one of the answers provide an answer to your question). You could post a self answer and accept that? – syntonym Jun 28 '16 at 12:51

1 Answers1

1

the possible duplicate link solved it, as syntonym pointed out in the comments and user Schien explains in one of the answers to the linked question:

the raw http post data (the stuff after the query) can be read through stdin. so the sys.stdin.read() method can be used.

my code now works looking like this:

#!/usr/bin/python3
import sys
import os

f = open('test','w')
f.write(str(sys.stdin.read()))
f.close()
gaugau
  • 765
  • 1
  • 9
  • 30