[Patches] [ python-Patches-545480 ] Examples for urllib2

SourceForge.net noreply at sourceforge.net
Sun Mar 21 14:34:23 EST 2004


Patches item #545480, was opened at 2002-04-18 00:13
Message generated for change (Comment added) made by tim_one
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=545480&group_id=5470

Category: Documentation
Group: None
>Status: Closed
Resolution: Accepted
Priority: 5
Submitted By: Sean Reifschneider (jafo)
Assigned to: Jeremy Hylton (jhylton)
Summary: Examples for urllib2

Initial Comment:
An associate who's learning Python recently complained
about a lack of
examples for urllib2.  As a starting point, I'd like to
submit the
following:

This example gets the python.org main page and displays
the first 100 bytes
of it:

   >>> import urllib2
   >>> url = urllib2.urlopen('http://www.python.org/')
   >>> print url.read()[:100]
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
   <html>
   <!-- THIS PAGE IS AUTOMATICAL
   >>>

Here we are sending a data-stream to the stdin of a CGI
and reading the
data it returns to us:

   >>> import urllib2
   >>> req = urllib2.Request(url =
&#039;https://localhost/cgi-bin/test.cgi&#039;,
   ...       data = &#039;This data is passed to stdin of
the CGI&#039;)
   >>> url = urllib2.urlopen(req)
   >>> print url.read()
   Got Data: "This data is passed to stdin of the CGI"
   >>>

The code for the sample CGI used in the above example is:

   #!/usr/bin/env python
   import sys
   data = sys.stdin.read()
   print &#039;Content-type: text-plain\n\nGot Data: "%s"&#039; %
data

----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2004-03-21 14:34

Message:
Logged In: YES 
user_id=31435

Since Fred said he checked in the examples here last year, 
and there is no other actionable item in this patch report, 
closing it now.

----------------------------------------------------------------------

Comment By: John J Lee (jjlee)
Date: 2003-08-31 15:49

Message:
Logged In: YES 
user_id=261020

Please see 798244 -- I've uploaded some more examples 
there. 

----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2003-04-25 11:42

Message:
Logged In: YES 
user_id=3066

I've committed the examples from the patch in a slightly
modified form to Doc/lib/liburllib2.tex revisions 1.8 and
1.6.8.2.

Additional examples would be really helpful; especially
demonstrating the authentication support, as noted in the
comment from "fresh".

----------------------------------------------------------------------

Comment By: Chris Withers (fresh)
Date: 2002-11-19 11:02

Message:
Logged In: YES 
user_id=24723

The examples for urllib2 are still really lacking :-(

I want to open an http basic auth protected URL and check
whether or not I get a 404 back. I have no idea hwo to do
that given the current documentation...

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=545480&group_id=5470



More information about the Patches mailing list