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

2009-03-02 Thread 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:

>>> f = open('/tmp/workfile', 'w')
>>> print f

>>> f.read()
'This is the entire file.\n'
>>> f.close()


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.

>>> import httplib, urllib
>>> params = urllib.urlencode({'textarea1': 0})
>>> 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
302 Found
>>> data = response.read()
>>> conn.close()



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
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-03-02 Thread JohnV
Here is what var data collected:


302 Found

Found
The document has moved http://www.thenational.us/pages/
htmlos/001863.1.059070780420726458">here.

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

--
http://mail.python.org/mailman/listinfo/python-list


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

2009-03-02 Thread 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 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()
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-03-02 Thread 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.



--
http://mail.python.org/mailman/listinfo/python-list


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

2009-03-04 Thread JohnV
On Mar 2, 10:13 pm, JohnV  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.
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-03-06 Thread 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.
>
> --
> Gabriel Genellina

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.


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



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.
--
http://mail.python.org/mailman/listinfo/python-list


File Compare with difflib.context_diff

2009-03-18 Thread JohnV
I have a txt file that gets appended with data over a time event.  The
data comes from an RFID reader and is dumped to the file by the RFID
software.  I want to poll that file several times over the time period
of the event to capture the current data in the RFID reader.

When I read the data I want to be able to compare the current data to
the date from the last time I read it and only process the data
appended since the last time it was read.

The first time I read the data, it might look like this:
AU08JEDD011485H14472402210
AU08JEDD020163C14472502210
AU08JEDD005029C14480102210
AU08JEDD004923H14482002210
AU08AWOL000799H14483902210

The next time it might look like this (with data appended to it)
AU08JEDD011485H14472402210
AU08JEDD020163C14472502210
AU08JEDD005029C14480102210
AU08JEDD004923H14482002210
AU08AWOL000799H14483902210
AU08AWOL000120H14495902210
AU08ARPU050241H14511702210
IF08DRTO008074H14520202210
IF08DRTO008089H14521102210
IF08DRTO008077H14553602210
IF08CHES23H14594902210

What I want to do is compare the old data (lets day it is saved to a
file called 'lastdata.txt') with the new data (lets day it is saved to
a file called 'currentdata.txt') and save the new appended data to a
variable which I HTTP POST to a website where I process the data for
display to interested parties.  In the example below I am trying to
save the new appended data to a file called "out.txt"

I have looked at difflib.context_diff but I cannot get the syntax
correct.  This is what I have taken from an example from this page
http://docs.python.org/library/difflib.html.  One thing I do not
understand is what do I do with: fromfile='before.py',
tofile='after.py' in the example code.

**

import sys
import difflib

sys.stdout = open("out.txt","w")


f1 = open(r'C:\Users\Owner\Desktop\lastdata.txt', 'r')
read_data1 = f1.read()
f1.close()

f2 = open(r'C:\Users\Owner\Desktop\currentdata.txt', 'r')
read_data2 = f2.read()
f2.close()

for line in context_diff(read_data1, read_data2, fromfile='before.py',
tofile='after.py'):
sys.stdout.write(line)


***

for line in context_diff(read_data1, read_data2, fromfile='before.py',
tofile='after.py'): is the line that causes the syntax error.

I would hope that when the script worked that "out.txt" would have the
appended data.  I would then copy currentdata.txt to lastdata.txt.  No
need to clear out the data in currentdata.txt as the next dump will
overwrite that data.

Any help or insights appreciated, thanks...

--
http://mail.python.org/mailman/listinfo/python-list


Re: File Compare with difflib.context_diff

2009-03-18 Thread JohnV
Maybe something like this will work though I am not sure of my quotes
and what to import

import shutil

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

shutil.copy('C:\Users\Owner\Desktop\newdata.txt', 'C:\Users\Owner
\Desktop\out.txt')
file = open(r'C:\Users\Owner\Desktop\out.txt', 'w')
file.write(text.replace(read_data1, ""))
file.close()
--
http://mail.python.org/mailman/listinfo/python-list


Re: File Compare with difflib.context_diff

2009-03-18 Thread JohnV
The below code does the trick with one small problem left to be solved

import shutil
import string

currentdata_file = r"C:\Users\Owner\Desktop\newdata.txt" # the current
download from the clock
lastdata_file = r"C:\Users\Owner\Desktop\mydata.txt" # the prior
download from the clock
output_file = r"C:\Users\Owner\Desktop\out.txt" # will hold delta
clock data

shutil.copy(currentdata_file, output_file)

f = open(lastdata_file, 'r')
read_data1 = f.read()
f.close()

f = open(currentdata_file, 'r')
read_data2 = f.read()
f.close()

replaceText = ''

file = open(output_file, 'w')
file.write(string.replace(read_data2, read_data1, replaceText))
file.close()

Contents of lastdata_file:
AU08JEDD011485H14472402210
AU08JEDD020163C14472502210
AU08JEDD005029C14480102210
AU08JEDD004923H14482002210
AU08AWOL000799H14483902210

Contents of currentdata_file:
AU08JEDD011485H14472402210
AU08JEDD020163C14472502210
AU08JEDD005029C14480102210
AU08JEDD004923H14482002210
AU08AWOL000799H14483902210
AU08AWOL000120H14495902210
AU08ARPU050241H14511702210
IF08DRTO008074H14520202210
IF08DRTO008089H14521102210
IF08DRTO008077H14553602210
IF08CHES23H14594902210

Contents of output_file:

AU08AWOL000120H14495902210
AU08ARPU050241H14511702210
IF08DRTO008074H14520202210
IF08DRTO008089H14521102210
IF08DRTO008077H14553602210
IF08CHES23H14594902210

output_file has a blank line at the top because of replaceText = ''
Is there something besides '' that I can use to not end up with a
blank line at the top of output_file, or what is the code to delete
the first line of that file?

Thanks
--
http://mail.python.org/mailman/listinfo/python-list


Re: File Compare with difflib.context_diff

2009-03-19 Thread JohnV
Here is the latest version of the code:

currentdata_file = r"C:\Users\Owner\Desktop\newdata.txt" # the latest
download from the clock
lastdata_file = r"C:\Users\Owner\Desktop\mydata.txt" # the prior
download from the clock
output_file = r"C:\Users\Owner\Desktop\out.txt" # will hold delta
clock data

newdata = open(currentdata_file).read()[len(open(lastdata_file).read
()):]
newdata2 = newdata.strip()


file = open(output_file, 'w')
file.write(newdata2)
file.close()


Do I need to close currentdata_file and lastdata_file ?

Have not gotten the os.stat example to work yet...
thanks for the help.
--
http://mail.python.org/mailman/listinfo/python-list