How do I post this HTML form using Python?
<p>This is the form that I want to post in Python:</p>

<pre><code>  &lt;FORM METHOD="POST"
    ACTION="http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run"
    ENCTYPE="multipart/form-data"&gt;
    &lt;INPUT NAME="formtype" TYPE="HIDDEN" value="simple"&gt;

    &lt;p&gt;&lt;b&gt;Upload a sentence corpus file&lt;/b&gt;:&lt;br&gt; 
      &lt;INPUT NAME="corpus" TYPE="FILE" SIZE=60 VALUE="empty"&gt;
    &lt;/p&gt;

      &lt;INPUT TYPE="submit" VALUE="COMPILE KNOWLEDGE BASE"&gt;
    &lt;/form&gt;
</code></pre>

<p>i tried it with requests</p>

<pre><code>import requests

url = "http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run"
#url = "http://httpbin.org/post"
files = {'file': open('testfile', 'rb')}
payload = {'NAME': 'fromtype', 'TYPE': 'HIDDEN', 'value': 'simple',
'NAME': 'corpus', 'TYPE': 'FILE', 'SIZE': '60', 'VALUE': 'empty'}
r = requests.post(url, data=payload)

print r.text
</code></pre>

<p>and the response</p>

<pre><code>Running: /home/darthtoker/programs/post.py (Fri Oct  7 15:19:21 2016)

&lt;!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"&gt;
&lt;head&gt;
&lt;title&gt;LMTool Error&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;pre&gt;Something went wrong. This is all I know: formtype
&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;Content-Type: text/html; charset=ISO-8859-1

&lt;!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"&gt;
&lt;head&gt;
&lt;title&gt;LMTool Error&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;pre&gt;Something went wrong. This is all I know: corpus
&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;Status: 302 Found
Location: http://www.speech.cs.cmu.edu/tools/product/1475867962_31194
</code></pre>

<p>I need to automate this form for a project I'm working on, please help!</p>

<p>There might be other errors, but the error message you get is clear enough:</p>

<p>What the form expects:</p>

<pre><code>&lt;INPUT NAME="formtype" ...&gt;
</code></pre>

<p>what you send:</p>

<pre><code>'NAME': 'fromtype', ...
</code></pre>

<p>You see the difference between <strong>formtype</strong> and <strong>fromtype</strong>?</p>

<hr>

<p>According to the documentations of <a href="http://docs.python-requests.org/en/master/" rel="nofollow">Requests</a> (<a href="http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests" rel="nofollow">More complicated POST requests</a>, <a href="http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file" rel="nofollow">POST a Multipart-Encoded File</a>) your code should be:</p>

<pre><code>import requests

url = "http://www.speech.cs.cmu.edu/cgi-bin/tools/lmtool/run"
#url = "http://httpbin.org/post"
files = {'corpus': open('testfile', 'rb')}
payload = {'formtype': 'simple' }
r = requests.post(url, data=payload, files=files)

print r.text
</code></pre>

python html post python-requests