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

JohnV loftmaster at gmail.com
Wed Mar 4 11:36:21 EST 2009


On Mar 2, 10:13 pm, JohnV <loftmas... at gmail.com> wrote:
> 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 ishttp://www.thenational.us/pages/start/test/getpost.htmlso 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.


Couldn't figure out the proper usage of the urllib2 functions to fix
the 302 Found problem, but what I did was change the URL to a php page
and httplib.HTTPConnection() worked fine when a "POST" was sent to
that page.  So, when I have learned a bit more about Python, I may
well go back and try to resolve the problem listed here. Thanks
Gabriel, for the assistance you did provide.



More information about the Python-list mailing list