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

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Mar 3 06:01:37 EST 2009


En Tue, 03 Mar 2009 04:13:46 -0200, JohnV <loftmaster at gmail.com> escribió:

> 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()

\ is the escape character in Python. You must double it when you want it  
to stand by itself:
'C:\\Users\\Owner\\Desktop\\mydata.txt'
Alternatively, use a raw string: r'C:\Users\Owner\Desktop\mydata.txt'
The same applies to the output file.

> 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"

Yes!

> 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?

Open a command prompt first (Start > Run > type "CMD" and enter), go to  
the directory where your script is located (type: CD c:\some\directory)  
and then you're ready to execute it using: python scriptname.py

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

No, it should be response.close() instead. The whole script is now:

<code>
import httplib, urllib, urllib2

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

params = urllib.urlencode({'textarea1': read_data})
headers = {"Content-type": "application/x-www-form-urlencoded",
            "Accept": "text/plain"}
url = "http://www.thenational.us:80/pages/start/test/getpost.html"
req = urllib2.Request(url, params, headers)
response = urllib2.urlopen(req)
data = response.read()
response.close()
print data

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

-- 
Gabriel Genellina




More information about the Python-list mailing list