[Baypiggies] Using urllib2 to print file contents line-by-line

Purui Wang puruiw at yahoo.com
Sat Dec 19 22:48:31 CET 2009


some samples here
http://nullege.com/codes/search?cq=urllib.request.urlopen

- Purui




________________________________
From: ken barclay <ken.barclay at att.net>
To: baypiggies at python.org
Sent: Sat, December 19, 2009 10:40:59 AM
Subject: Re: [Baypiggies] Using urllib2 to print file contents line-by-line


I'm running the backend on a Centos box.

I do have a workaround but it requires an extra step from the user - I just print out the url on the output page, and when the user clicks the link, the browser takes care of loading the file into the browser and it looks exactly like it does in the file.

Would be nice to avoid that step but it's not critical - Thanks Aahz.

--- On Sat, 12/19/09, Aahz <aahz at pythoncraft.com> wrote:


>From: Aahz <aahz at pythoncraft.com>
>Subject: Re: [Baypiggies] Using urllib2 to print file contents line-by-line
>To: baypiggies at python.org
>Date: Saturday, December 19, 2009, 4:22 PM
>
>
>On Fri, Dec 18, 2009, ken barclay wrote:
>>
>> I'm trying to do
> something simple - I want to print the contents of
>> files that are staged on a web server back to the browser. I loop over
>> the file-like object that comes back in the response:
>>
>> ??????? import urllib2
>> ??????? req = urllib2.Request(href)
>> ??????? response = urllib2.urlopen(req)
>> ??????? for line in response:
>> ??????? print line
>> 
>> But I want the lines to appear in the browser exactly like they do
>> in the file - with each one on a new line (because it's columnar
>> data.) The problem is: this approach doesn't work because the first
>> 'print' is printing out the whole response in one big stream (ignoring
>> newlines).
>
>What OS are you on?  You are probably running into newline variations,
>and I'm not sure whether you can set universal newlines on an
>already-opened file.
>-- 
>Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/
>
>Looking back over the years, after I learned Python I realized that I
>never really had enjoyed programming before.
>_______________________________________________
>Baypiggies mailing list
>Baypiggies at python.org
>To change your subscription options or unsubscribe:
>http://mail.python.org/mailman/listinfo/baypiggies
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20091219/824ca6cb/attachment.htm>


More information about the Baypiggies mailing list