How to get POST parameters with Python?
Question: 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?)


Answer: You are sending it as POST. To obtain POST superglobal 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"]
    

