Using httplib to read a file online

Oyvind Ostlund Oyvind.Ostlund at
Tue Jun 14 17:35:39 CEST 2005

I am trying to read a file online, and was just testing a bit. This is what I tried.

import sys, httplib
showlines = 6

    servername, filename = sys.argv[1:]           # cmdline args?
    servername, filename = '', '/index.php'

print servername, filename
server = httplib.HTTP(servername)                 # connect to httpsite/server
server.putrequest('GET', filename)                # send request andheaders
server.putheader('Accept', 'text/html')           # POST requests workhere too
server.endheaders()                               # as do cgi script
file names 

errcode, errmsh, replyheader = server.getreply()  # read reply info headers
if errcode != 200:                                # 200 means success
    print 'Error sending request', errcode
    print 'Message', errmsh
    print 'RepHeader', replyheader
    file = server.getfile()                       # file obj for data
    data = file.readlines()
    file.close()                                  # show lines with eolnat end
    for line in data[:showlines]: print line,     # to save, write datato file 

There is a server called and a file called index.php on it, but why doesn't it work. If I changed it to '' and 'index.php' then it worked. What is the difference? Anyone have a qlue?''

Thanks in advance
- ØØ -

More information about the Python-list mailing list