New User - Using tutorial and Docs - POST returns 302 Found

JohnV loftmaster at gmail.com
Tue Mar 3 01:13:46 EST 2009


Thanks for your suggestion, but I am not able to get it to work for
me.

My original script was:

f = open('C:\Users\Owner\Desktop\mydata.txt', 'r')
read_data = f.read()

f.close()

import httplib, urllib
params = urllib.urlencode({'textarea1': read_data})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}

conn = httplib.HTTPConnection("thenational.us:80")
conn.request("POST", "/pages/start/test/getpost.html", params,
headers)
response = conn.getresponse()

print response.status, response.reason
data = response.read()
conn.close()

f = open('C:\Users\Owner\Desktop\pydata.txt', 'a')
f.write(data)
f.close()


I think that what you wanted me to change it to is:

f = open('C:\Users\Owner\Desktop\mydata.txt', 'r')
read_data = f.read()

f.close()

import httplib, urllib, urllib2

params = urllib.urlencode({'textarea1': read_data})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}

url = "http://thenational.us:80/pages/htmlos/
001863.5.083914970120726458"
req = urllib2.Request(url, params, headers)
response = urllib2.urlopen(req)

print response.status, response.reason
data = response.read()
conn.close()

f = open('C:\Users\Owner\Desktop\pydata.txt', 'a')
f.write(data)
f.close()


I imported urllib2 However, this the scriot does not seem to work for
me for two reasons

First the url that recieves the post is
http://www.thenational.us/pages/start/test/getpost.html so I belive
the "url line" should read:

url = "http://www.thenational.us:80/pages/start/test/getpost.html"

I do not know how to capture the text that is displayed in the
interpreter window when I run the program, as the window closes after
the script is run.  How do I keep the window open so I can read what
messages are printed there?

Finally, do I need conn.close() with the new code you suggested?

Any help appreciated.






More information about the Python-list mailing list