Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6070869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:58:56+00:00 2026-05-23T09:58:56+00:00

I’m trying to learn how urllib2 works and how it encapsulates its various components

  • 0

I’m trying to learn how urllib2 works and how it encapsulates its various components before sending out an actual request or response.

So far I have:

theurl = "www.example.com"

That obviously specifies the URL to look at.

req = urllib2.Request(theurl) 

Don’t know what this does, hence the question.

handle = urllib2.urlopen(req)

This one gets the page and does all the requests and responses required.

So my question is, what does urllib2.Request actually do?

To try and look at it to get an idea I tried

print req 

and just got

<urllib2.Request instance at 0x123456789>

I also tried

print req.read() 

and got:

Traceback (most recent call last):  
    File "<stdin>", line 1, in ?  
    File "/usr/lib64/python2.4/urllib2.py, line 207, in `__`getattr`__`  
        raise AttributeError, attr  
AttributeError: read

So I’m obviously doing something wrong. If anyone can help in one of both my questions that would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:58:57+00:00Added an answer on May 23, 2026 at 9:58 am

    The class “Request” you’re asking about:
    http://docs.python.org/library/urllib2.html#urllib2.Request

    class urllib2.Request(url[, data][,
    headers][, origin_req_host][,
    unverifiable])

    This class is an abstraction of a URL
    request.

    The function you actually want to make a request (which can accept a Request object or wrap one around a URL string you provice) constructing a Request object): http://docs.python.org/library/urllib2.html#urllib2.urlopen

    urllib2.urlopen(url[, data][,timeout])
    Open the URL url, which can be either a string or a Request object.

    Example:

    theurl = "www.example.com"
    try:
        resp = urllib2.urlopen(theurl)
        print resp.read()
    except IOError as e:
        print "Error: ", e
    

    Example 2 (with Request):

    theurl = "www.example.com"
    try:
        req = urllib2.Request(theurl)
        print req.get_full_url()
        print req.get_method()
        print dir(req)  # list lots of other stuff in Request
        resp = urllib2.urlopen(req)
        print resp.read()
    except IOError as e:
        print "Error: ", e
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.