I am having some problems with python. If I use IE to POST data to a page using a simple form, I get the following

POST /automa/auth.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Referer: http://maroon5/automa/login.php
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: maroon5
Content-Length: 44
Connection: Keep-Alive
Cache-Control: no-cache
fuser=jchaney&fpassword=n0p@ss&Submit=Submit

HTTP/1.1 200 OK
Date: Wed, 01 Apr 2009 20:32:59 GMT
Server: Apache/2.2.9 (Fedora)
X-Powered-By: PHP/5.2.6
Content-Length: 355
Connection: close
Content-Type: text/html; charset=UTF-8
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Automa - Form Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>Data is jchaney and n0p@ss</body>
</html>

But if I try the samething using the following script.

#!/usr/bin/python2
"""Connect and login
"""
import urllib
import urllib2
import httplib
params = urllib.urlencode({'fuser': 'jchaney', 'fpassword': 'n0p@ss'\
, 'submit': 'submit'})
headers = {"Content-type": "application/x-www-form-urlencoded",\
"Accept": "text/plain"}
conn = httplib.HTTPConnection('192.168.1.90:80')
conn.request("POST", "automa/auth.php", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()

I get a 400 Bad Request fault, see dump.

POST automa/auth.php HTTP/1.1
Host: 192.168.1.90
Accept-Encoding: identity
Content-Length: 46
Content-type: application/x-www-form-urlencoded
Accept: text/plain
fuser=jchaney&fpassword=n0p%40ss&submit=submit

HTTP/1.1 400 Bad Request
Date: Wed, 01 Apr 2009 20:18:27 GMT
Server: Apache/2.2.9 (Fedora)
Content-Length: 303
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.2.9 (Fedora) Server at 192.168.1.90 Port 80</address>
</body></html>

I am not sure what I am doing wrong in my code.
Thank you for any ideas.

Umm.. i would edit those passwords there. Just asterisk them or something.

The passwords are fake and the system is intraneted, no external connections to the test box. Thank you for the concern though.

More information about error.
I set the httpd.conf error logging to debug and re-ran the script.
Results were:
[error] [client 192.168.1.153] Invalid URI in request POST automa/auth.php HTTP/1.1, referer:

Is there something special in the header that I am missing?

Here is the answer, thanks too Jardel Weyrich.

The line of code

CONN.request("POST", "automa/auth.php", PARAMS, HEADERS)

is wrong, it should be

CONN.request("POST", "/automa/auth.php", PARAMS, HEADERS)

And http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html is the reference manual too go by.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.