#1
  1. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    23
    Rep 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/li...e-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 11:34 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    netherlands
    Posts
    51
    Rep Power
    12
    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.
  4. #3
  5. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    23
    Rep 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 11:51 AM.
  6. #4
  7. Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    23
    Rep 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/li...le-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 12:48 PM.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    Last edited by netytan; February 5th, 2004 at 05:17 PM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo