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

Jeremy Fishman jeremy.r.fishman at gmail.com
Sun Dec 20 05:59:41 CET 2009


Maybe I'm missing something, but if you're rendering an http response in a
browser as text, why not set the Content-Type to text/plain?

  - Jeremy

On Sat, Dec 19, 2009 at 5:41 PM, Adam Hupp <adam at hupp.org> wrote:

> HTML will not generally display embedded newlines in text.  Alans's
> suggestion of wrapping the output in <pre> is good, or you could do
> something like:
>
> response.read().split("\n").join("<br/>")
>
> to insert HTML linebreaks.  If the data is truly columnar though a <table>
> would be most appropriate, though that would be slightly more complicated.
>
> -Adam
>
> On Fri, Dec 18, 2009 at 7:39 PM, ken barclay <ken.barclay at att.net> wrote:
>
>> Hello,
>>
>> 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).
>>
>> I tried the other techniques at
>> http://docs.python.org/library/urllib2.html still get the same result.
>> Suggestions?
>>
>> Thanks
>> Ken
>>
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/mailman/listinfo/baypiggies
>>
>
>
>
> --
> Adam Hupp | http://hupp.org/adam/
>
> _______________________________________________
> 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/a3cb2689/attachment.htm>


More information about the Baypiggies mailing list