Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old February 5th, 2004, 10:24 AM
rickt's Avatar
rickt rickt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 rickt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Basic Python Questions: My first program

Hi all,

I'm not a programmer but I'd like to learn. I've got enough background to be able to script simple stuff in perl but that's about all.

Right now I've got a project I'd like to convert into an all python solution. Currently I have a batch script set up to netcat out to a server, grab a web page, compare it to a copy on the local file system, make some file changes, and then write a file to the users desktop if the web page has changed.

Basic stuff I would think. So, the first part I would assume that I need to learn is how does python perform HTTP connections and can it interact with CGI programs? The current netcat I've got scripted goes to a specific cgi page, passes it some data and writes the output to a file.

What I'd like to know is where can I find specific python information that'll help me to understand how to write that portion of the code. **I'm not looking for the answer; I just need a way to narrow my search.** Google just gives back waaaay too much information and most of it is over my head. I'm just looking for the basics.

Incidentally, I'm using python 2.3. I've been using the supplied GUI (TKsomething) but I've seen threads on wxPython that supposed to make it easier to program GUIs around your python scripts. I've installed wxPython 2.4 but I'm already intimidated by the 4 or 5 different py(pie) development environments. (If that's what they are!?) The python program I'm looking to develop is going to be command line only, but I figured I could learn to use a new tool if it would help to make it more familiar for me if I tried programming more complex stuff that needed a GUI.

Sorry for being long winded. I wanted to supply as much info as possible.

Thanks in advance for any guidance or advice.

-Rick

UPDATE: PS. Currently I'm reading up on http://www.python.org/doc/current/l...le-urllib2.html

Not sure if this is the path I should be following but it's the one I'm on.

Last edited by rickt : February 5th, 2004 at 10:34 AM.

Reply With Quote
  #2  
Old February 5th, 2004, 10:35 AM
darkleaf darkleaf is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: netherlands
Posts: 51 darkleaf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I think you want to look through some tutorials on how to write a bit. Start with the basic ones and continue to what you thin you can manage. I think most python resource sites have some basic tutorials with some easy examples. If you already know this I think you need to search the python documentation, there's a full list of functions in it where you can choose what you need.

Reply With Quote
  #3  
Old February 5th, 2004, 10:48 AM
rickt's Avatar
rickt rickt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 rickt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Past Research

darkleaf:

I've gone through the book at:
http://www.honors.montana.edu/~jjc/easytut/easytut/

It helped a lot. I'm pretty sure I understand the basics. I just want to apply what I learned now. Currently I'm able to make a connection to a web site but I'm not sure if I can make the connection a POST and then give some data to the waiting CGI script on the remote web server. I'm still trying to find that information on the bit "W".

I'm using URLLIB2.

-Rick

Last edited by rickt : February 5th, 2004 at 10:51 AM.

Reply With Quote
  #4  
Old February 5th, 2004, 11:44 AM
rickt's Avatar
rickt rickt is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 rickt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post Comment this program?

Can anyone place some comments in this piece of code? I've been going back and forth from the code and this web site http://www.python.org/doc/current/l...ule-urllib.html but I don't understand all of it.

Code:
import httplib, urllib
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
h = httplib.HTTP("www.musi-cal.com:80")
h.putrequest("POST", "/cgi-bin/query")
h.putheader("Content-length", "%d" % len(params))
h.putheader('Accept', 'text/plain')
h.putheader('Host', 'www.musi-cal.com')
h.endheaders()
h.send(params)
reply, msg, hdrs = h.getreply()
print reply # should be 200
data = h.getfile().read() # get the raw HTML


Specifically what is the line:
Code:
reply, msg, hdrs = h.getreply()
doing?

Again, thanks for any help you can offer. I've munged the cod above to perform what I need it to (the parameters are sent backwords... first is sent last and the last is sent first... interesting). I'm trying to figure out how to write the data to a file nicely.

EDIT:
Added the following code to write to create a new file:

Code:
file_name = "webpage.html"
mynew_file = open(file_name,'w')
mynew_file.write(data)
print "It worked!"


Can the small above section be made more concise? It looks like it should be able to be.

Last edited by rickt : February 5th, 2004 at 11:48 AM.

Reply With Quote
  #5  
Old February 5th, 2004, 04:12 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I could go though and comment it but most if not all what you're trying to do (as i understand it) can be done with just urllib so theres very little point...

http://www.python.org/doc/2.3.3/lib/module-urllib.html

1. connect to the server via urllib.urlopen() and get the last modifide data.

This is fine for none dynamic webpages. And if the page can accept values from GET then you can just pass values along with the URL.

Code:
>>> import urllib
>>> info = urllib.urlopen('http://www.python.org/').info()
>>> print info
Age: 9717
Accept-Ranges: bytes
Date: Thu, 05 Feb 2004 19:15:20 GMT
Content-Length: 10821
Content-Type: text/html
Server: Apache/1.3.26 (Unix)
Last-Modified: Mon, 26 Jan 2004 17:44:33 GMT
ETag: "5a757d-2a45-40155201"
Via: 1.1 webcacheB05 (NetCache NetApp/5.3.1R4)

>>> info['Last-Modified']
'Mon, 26 Jan 2004 17:44:33 GMT'
>>> 


2. store the last modifide date in a file so you can compare against it latter.

Code:
>>> file('filename.txt', 'w').write('last modifide date')


3. when checking simply open the file and read in the stored date. Then compare it to the wepages last modifide date!

Code:
>>> stored_date != last_modifide


You should also check out the Python tutorial at http://www.python.org/doc/2.3.3/tut/tut.html. I have a simpler example of sending data to a webpage using POST somewhere; i'll have a look for you.

Hope this helps,

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : February 5th, 2004 at 04:17 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Basic Python Questions: My first program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway