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

noreply@sourceforge.net noreply@sourceforge.net
Tue, 19 Nov 2002 08:02:55 -0800


Patches item #545480, was opened at 2002-04-18 04:13
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=545480&group_id=5470

Category: Documentation
Group: None
Status: Open
Resolution: None
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 =
'https://localhost/cgi-bin/test.cgi',
   ...       data = 'This data is passed to stdin of
the CGI')
   >>> 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 'Content-type: text-plain\n\nGot Data: "%s"' %
data

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

Comment By: Chris Withers (fresh)
Date: 2002-11-19 16: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