How to send a POST request to a .php page in python
<p>Context:
So I'm trying to build a python program that will send a POST request to a specific .php file, and return the output. I've done a little bit of research, and this is the code I have so far:</p>

<pre><code>def ForcePush():
 params = urllib.urlencode({'log': 'admin', 'pwd':'password'})
 headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain", "Accept-Language":"en-Us,en;q=0.5" ,
 "Referer":"http://192.168.18.138/wp-login.php"}
 conn = httplib.HTTPConnection(raw_input("Where would you like to browse to: "))

 conn.request("POST", "", params, headers)
 response = conn.getresponse()
 data = response.read()
 print data
 conn.close()
</code></pre>

<p>The code works fine for a normal website, like www.google.com, but if I try to go to a php page I get this error: </p>

<pre><code>Traceback (most recent call last):
File "WPEnum.py", line 24, in &lt;module&gt;
ForcePush()
File "WPEnum.py", line 18, in ForcePush
conn.request("POST", "", params, headers)
File "C:\Python27\lib\httplib.py", line 1057, in request
self._send_request(method, url, body, headers)
File "C:\Python27\lib\httplib.py", line 1097, in _send_request
self.endheaders(body)
File "C:\Python27\lib\httplib.py", line 1053, in endheaders
self._send_output(message_body)
File "C:\Python27\lib\httplib.py", line 897, in _send_output
self.send(msg)
File "C:\Python27\lib\httplib.py", line 859, in send
self.connect()
File "C:\Python27\lib\httplib.py", line 836, in connect
self.timeout, self.source_address)
File "C:\Python27\lib\socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
</code></pre>

<p>In case you are wondering, I'm making the program to enumerate the WPAdmin for the Mr.Robot vulnverable VM on VMWare. Doing this for educational purposes. This is the request I'm trying to emulate:</p>

<pre><code>POST /wp-login.php HTTP/1.1
Host: 192.168.18.138
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101   Firefox/38.0 Iceweasel/38.8.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://192.168.18.138/wp-login.php
Cookie: s_fid=2692E4153C7D3D30-158A9B35CCC16635; s_nr=1473166726975; s_cc=true; s_sq=%5B%5BB%5D%5D; wordpress_test_cookie=WP+Cookie+check
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 104

log=admin&amp;pwd=login&amp;wp-submit=Log+In&amp;redirect_to=https%3A%2F%2F192.168.18.138%2Fwp-admin%2F&amp;testcookie=1
</code></pre>

<p>I know I don't have all of the headers, but that doesn't seem to be what the error is suggesting is wrong. Does anyone know what the problem is?</p>

<p>As you see from exception, you have connection-related error (on opening socket), so obviously host/port you're using in request is wrong, or inaccessible from python environment (and obviously from system) for some reason.</p>

<p>To debug try to get same page with curl or wget (even with GET method).
<code>curl http://my_host:my_port/my_page.php</code></p>

python python-2.7