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

J

JohnV

I am using Python 2.5 r25:51908 MSC v.1318 32 bit (Intel) on wind32

I am totally new to Python and started yesterday going over a couple
of examples I found in the documentation which address a problem I am
trying to solve.

I have successfully opened a file and read the results adapting this
code snippit:


Then I tried to POST something to a cgi sytle script on my test
website. Below is the code I used, I type it in one line at time to
make sure I am doing it correctly. I am confused about how to format
several of the lines in the below script.

on the params line the name / value pair I want to send is name =
'textarea1' value = 0
on the conn line I put "thenational.us' as the domain
on the conn.request line I put the web path to the html page
(getpost.html) that processes the POST

The data is not being posted to the webpage. Concerning the
conn.request line, "/pages/" is what I call the cgi-bin directory,
"start" is the name of the cgi script I use (no ext), "/test/
getpost.html" is the web path to the page that accepts the POST. I do
not use GET because the data will be several k or larger in size when
I get it all working correctly.



My project I want to learn how to do is; I have data coming from an
external device over a com port that I manually download and save to a
file on my home computer with the manufacturer's software. I then
want to move that data to my webserver where others can view the data
from a webpage. My goal is to open a file on my home computer and
load the contents of that file to my website where I will store the
data and display it. I could just copy the file to the webserver that
would accomplish the same thing as posting it as a var and saving that
to a file on the server. Howerver, I am trying to automate the
process as much as possible with Python as when an event is happening,
new data is coming in over a period of seveal hour from the external
device over the com port.

Is there a better solution than POST
 
J

JohnV

Here is what var data collected:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.thenational.us/pages/
htmlos/001863.1.059070780420726458">here</a>.</p>
<hr>
<address>Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8b mod_mono/
2.2 FrontPage/5.0.2.2635 mod_bwlimited/1.4 mod_auth_passthrough/2.1
Server at thenational.us Port 80</address>
</body></html>
 
J

JohnV

I got it! You can see the code at the bottom of this post. Sorry for
three posts on this question.

I have to run the program one time just to get the dynamically
generated redirect URL for the POST (it looks like this)
The document has moved <a href="http://www.thenational.us/pages/htmlos/
001863.1.059070780420726458">

I then paste the redirected URL ("http://www.thenational.us/pages/
htmlos/001863.1.059070780420726458) into the script and run it again
and it works.

My question now is how do I bypass this manual step and grab the
redirect URL automatically without human intervention. In other
words, how do I get the python script to find out what the redirect
URL is going to be (it is dynamic and times out after 50 minutes if
idle) and automatically update the script with the redirect URL?

Also, I was able to figured out how to open a file and send the data
so almost all my work is done, excect for this pesky URL redirect on
the POST.

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/htmlos/001863.5.083914970120726458",
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()
 
G

Gabriel Genellina

I have to run the program one time just to get the dynamically
generated redirect URL for the POST (it looks like this)
The document has moved <a href="http://www.thenational.us/pages/htmlos/
001863.1.059070780420726458">

I then paste the redirected URL ("http://www.thenational.us/pages/
htmlos/001863.1.059070780420726458) into the script and run it again
and it works.

Let urllib2 handle the redirect for you.
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/htmlos/001863.5.083914970120726458",
params, headers)
response = conn.getresponse()

Replace the above three lines with:

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

For more info on using urllib2, see
http://www.voidspace.org.uk/python/articles/urllib2.shtml
 
J

JohnV

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.
 
J

JohnV

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.
 
G

Gabriel Genellina

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.

Sorry, I think you didn't get my later post -- than in fact was from a
month ago...
See http://article.gmane.org/gmane.comp.python.general/613312/
urllib2 should handle a 302 redirect automatically.
 
J

JohnV

Sorry, I think you didn't get my later post -- than in fact was from a  
month ago...
Seehttp://article.gmane.org/gmane.comp.python.general/613312/
urllib2 should handle a 302 redirect automatically.

I went to the link you provided and sure enough there was your post.
However, the date was all wrong. I did not even submit to this list
until March 2,2009 so the date on your other post was probably in
European format 03/02/2009 but read as US 02/03/2009. In any case,
thank you. The code does get my request to the page however the data
sent in the POST does not arrive with the request.

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


I even changed the params line to to:
params = urllib.urlencode({'textarea1': 'somedata'})

then I tried taking part of the header out ("Accept": "text/plain")
like this:
headers = {"Content-type": "application/x-www-form-urlencoded"}

I still get to the getpost.html page because response has the html
from that page, but textarea1 does not get passed.

Putting :80 or leaving it out of the URL does not seem to matter
either way I get to the getpost page, just no data is passed.

I am running Python 2.5 r25:51908 MSC v.1318 32 bit (Intel) on wind32
and after doing a search on Python Post problems, I read that urllib2
handled 302 redirect POST incorrectly at least in 2.5x . See
http://bugs.python.org/issue1401

Maybe my code is still lacking some command, or I need to install a
newer version of Python. That option (installing) seems a little
daunting to me as I have no experience but I can learn if that will
fix the problem for me.

Thanks for the help so far, any furhter suggestions appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top