473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

urllib's functionality with urllib2

Hello,
Sorry for this maybe stupid newbie question but I didn't find any
answer in all my readings about python:

With urllib, using urlretrieve, it's possible to get the number of
blocks transferred and the total size of the file.

Is it possible to get those informations with urllib2 ?

( I have to use urllib2 and a Request Object with headers, urlretieve
doesn't work. Well, at least, all my attempts with urllib were
denied...)

Thanks for any help,

Monty

Jul 18 '05 #1
4 1624
Is there any reason why you can't import both?

import urllib as u
import urllib2 as uu

Jul 18 '05 #2

Monty wrote:
Hello,
Sorry for this maybe stupid newbie question but I didn't find any
answer in all my readings about python:

With urllib, using urlretrieve, it's possible to get the number of
blocks transferred and the total size of the file.

Is it possible to get those informations with urllib2 ?

( I have to use urllib2 and a Request Object with headers, urlretieve
doesn't work. Well, at least, all my attempts with urllib were
denied...)

urllib2.urlopen(req) returns a handle on the URL. This handle has
various properties - if a content length header was sent it will
include that.

If you read from the url in a loop you can do this :

.. h = urllib2.urlopen(req)
.. print h.info()
.. page = ''
.. count = 0
.. n = 100 # number of bytes to read at a time
.. while True:
.. a = h.read(n)
.. if not a: break
.. count += len(a) # len(a) may not be same as n for final read
.. page += a # prob. better to append to a list and do
''.join(a) afterwards
.. print 'Now transferred %s bytes.' % count

Does this help ?

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml
Thanks for any help,

Monty


Jul 18 '05 #3
Thanks very much Fuzzy,
It's help a lot. That's exactly what I was looking for.
I didn't know much about those handle stuff and what to do with.
The next hurdle for me will be to understand and manage multiple
threads, and it's good to know that one can find such helpful advices
here.( and maybe also on your website, I'm just opening the main page
and it's look like if there was much to see and learn...)
Bye,
Monty

Jul 18 '05 #4
No, there is no reason. I think I can import both, but I can't download
the file I want using urllib.retrieve. And urllib2 lack the retrieve
method (as much as I know at least) which very easily allow you to get
a feedback of the download (transfert rate and so on). That was my
problem.
It's OK now, Fuzzy explain me how to do that. (I'm really a newbie in
this marvellous world of programming, you know...)
Thanks anyway to take the time to read my post and to answer it.
Bye,
Monty

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gary Feldman | last post by:
I think I've found a deficiency in the design of urllib related to https. In order to complete an https connection, it appears that URLOpener and hence FancyURLOpener require the key and cert...
1
by: Chris Lyon | last post by:
Could somebody please explain the difference between these two modules and explain why they are both required, and if there will ever be a unification of them?
3
by: Volker M. | last post by:
Hey, I want to open a list of URLs with Pythons urllib and the fuction open(URL) automatically. It is important that the program open ONLY normal http-sites and no https-sites with...
0
by: Pieter Edelman | last post by:
Hi all, I'm trying to submit some data using a POST request to a HTTP server with BASIC authentication with python, but I can't get it to work. Since it's driving me completely nuts, so here's...
11
by: Johnny Lee | last post by:
Hi, I was using urllib to grab urls from web. here is the work flow of my program: 1. Get base url and max number of urls from user 2. Call filter to validate the base url 3. Read the source...
0
by: Ali.Sabil | last post by:
hello all, I just maybe hit a bug in both urllib and urllib2, actually urllib doesn't support proxy authentication, and if you setup the http_proxy env var to...
5
by: John Nagle | last post by:
I thought I had all the timeout problems with urllib worked around, but no. socket.setdefaulttimeout is useful, but not always effective. I'm setting that to 15 seconds. If the host end won't...
6
by: O.R.Senthil Kumaran | last post by:
Hi, There is an Open Tracker item against urllib2 library python.org/sf/735515 which states that. urllib / urllib2 should cache the results of 301 (permanent) redirections. This shouldn't break...
0
by: johnpollard | last post by:
For some reason this script isn't working and I dont know what it is. I believe the problem lies in the following lines of code since the script works with a different website and username/password...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.