2

I would like to call my Python script from different location ( FIY: I will call it later from my paypal registration process).

My configuration: I am running my website on AmazonWebServices. IIS8. Python3

I am calling to my python with this simple HTML file:

<form role="form" action="http://www.mywebsite.nz/cgi-bin/mypythonfile.py" method="post">

    <label for="person_name">Person name</label>                    
    <input id="person_name" type="text" name="person_name">

    <label for="email_address">Email address</label>                    
    <input id="email_address" type="text" name="email_address">

    <button id="submit_button" type="submit" >submit</button>

</form>

Here is what I have in my mypythonfile.py:

import cgi, cgitb


#Create instance of FieldStorage
form = cgi.FieldStorage() 
cgitb.enable()


#Here I will collect all parameters
variable = ""
value = ""
allFields = ""
for key in form.keys():
    variable = str(key)
    value = str(form.getvalue(variable))
    allFields += variable + ":" + value + "   "

print(allFields)

But the result I get is with empty values:

<email_address>:<None>  <person_name>:<None>

p.s. When calling the .py from the same place where the HTML file is - everything works perfectly. The problem happens when I call the .py file from an external HTML.

How can I fix it? (maybe it is some sort of configuration I have to add e.g. to IIS?)

Yura
  • 2,381
  • 8
  • 33
  • 44

1 Answers1

2

You are sending it as POST. To obtain POST variables, try replacing:

form = cgi.FieldStorage()

with this:

form = cgi.FieldStorage(environ="post")

Or you can try this:

import sys, urllib
query_string = sys.stdin.read()
multiform = urllib.parse.parse_qs(query_string)

And now you can use this:

multiform["email_address"]
skywalker
  • 1,230
  • 14
  • 30
  • Thank you @less! (+1) . When replacing this line something breaks. (In my real script I send a confirmation mail to myself with the variables and their values. When I replace my line with your I stop receiving any mails). Maybe I should add some additional import so that this line will work? – Yura Aug 15 '15 at 08:40
  • Thank you@les. After putting the line I start getting the confirmation mail (meaning that it does not fail). HOWEVER, the parameters are empty - no param name and no value... nothing was found. Please advise. Thank you – Yura Aug 15 '15 at 09:45
  • 1
    The cgitb is good for debugging, but you have not started it, you need to do: cgitb.enable() – skywalker Aug 15 '15 at 13:23
  • Such solution got "stuck". After calling the page, it even did not return any answer and seems to be "thinking" (the round circle on firefox tab keeps circling and never stops...) :-( NOTE: this works perfectly when the HTML file and the python file are on the same webserver. However, when they are on different werservers there is a problema nd I can't retrieve the POST params – Yura Aug 15 '15 at 13:44
  • 1
    Try using a library for this. Follow this answer: http://stackoverflow.com/a/464977/4898487 – skywalker Aug 15 '15 at 13:50
  • Thank you. I thought maybe integrating Flask, but I have never used it and it seems that it would take my quite a while integrating it into the code. Wouldn't it be an overkill to integrate it just to retrieve params?? – Yura Aug 15 '15 at 13:52
  • 1
    Check if you are submitting right fields and retrieving right fields. – skywalker Aug 15 '15 at 13:54
  • What do you mean the "right"? In my question I just loop over everything I have. I dont retrieve a certain param by name. (p.s. my answer is almost the same as my real code, which I have isimplified to debug and find where the problem is). I suspect that maybe I should somehow configure the IIS to accept POST request with params.... should I? – Yura Aug 15 '15 at 13:55
  • 1
    Sorry. But maybe you are right, I have once read that some servers have it blocked. – skywalker Aug 15 '15 at 14:02
  • Could you advise how to configure the IIS8 to allow POST requests and also allow to pass parameters? – Yura Aug 15 '15 at 14:03
  • 1
    I am using apache but I have found this: http://stackoverflow.com/questions/3472429/how-do-i-configure-iis-to-accept-post-requests. But it's for iis6 and iis7, I hope this will work for iis8 too. – skywalker Aug 15 '15 at 14:06
  • thanks. I already have it. but still can not get the POST params... :( hopefully some other member will post an answer. Thank very much @les !! – Yura Aug 15 '15 at 14:12
  • 1
    I am sorry I did not find the answer. The answers code works excellent for me. – skywalker Aug 15 '15 at 14:17
  • thanks @les! I will hope someone will answer me... ! :)) – Yura Aug 15 '15 at 14:19