473,396 Members | 1,816 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 and sites that require passwds

Hello,

I'm doing a small website survey as a consultant for a company that has
a large private lan. Basically, I'm trying to determine how many web
sites there are on their network and what content the sites contain
(scary how they don't know this, but I suspect many companies are this
way).

Everything is going fine so far except for sites that require passwds
to be accessed. I don't want to view content on these sites, I only
want to note that they are passwd protected, make a list of them and
move on. The problem is that urllib hangs waiting for a username/passwd
to be entered. Is there a graceful way to deal with this?
Many thanks,
Bob

Jul 18 '05 #1
4 1149
USe urllib2 which will fail with an exception. You can trap this
exception and using the code attribute of the exception object,
determine why it failed. The error code for 'authentication required'
is 401.

Off the top of my head :

import urllib2
req = urllib2.Request(theurl)
try:
handle = urllib2.urlopen(req)
except IOError, e:
if not e.hasattr('code'):
print 'The url appears to be invalid.'
print e.reason
else:
if e.code == 401:
print theurl, 'is protected with a password.'
else:
print 'We failed with error code', e.code
HTH

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

Jul 18 '05 #2
damn... I'm losing my leading spaces.... indentation should be obvious
anyway... (everything below except is indented at least one step).
Fuzzy

Jul 18 '05 #3
On 23 Dec 2004 06:46:50 -0800, Fuzzyman <fu******@gmail.com> wrote:
damn... I'm losing my leading spaces.... indentation should be obvious We'll forgive you for that. It was from "top-of-your-head" ~;-)
anyway... (everything below except is indented at least one step).
Fuzzy

Its nice that urllib2 returns errcode to process further. doesn't
urllib do that?
Anyway i wanted to know if any website which is similar to CPAN
library website? I mean i want to be able find modules n stuff for
Python.. It would be really great to know.

Thanks.

--
cheers,
Ishwor Gurung
Jul 18 '05 #4

Ishwor wrote:
On 23 Dec 2004 06:46:50 -0800, Fuzzyman <fu******@gmail.com> wrote:
damn... I'm losing my leading spaces.... indentation should be obvious
We'll forgive you for that. It was from "top-of-your-head" ~;-)

Hey - I put the indentation in there... it just got stripped out when
it was posted ! :-)
anyway... (everything below except is indented at least one step).
Fuzzy

Its nice that urllib2 returns errcode to process further. doesn't
urllib do that?


The OP is saying that it hangs rather than returning an error. I
haven't tested it. In general urllib2.urlopen is much better than
urllib.urlopen. urllib has some useful other functions though.
Anyway i wanted to know if any website which is similar to CPAN
library website? I mean i want to be able find modules n stuff for
Python.. It would be really great to know.

There is PyPi and the Vaults of Parnassus. Neither are really like
CPAN. There has been lots of talk about it recently - everyone agrees
we need one... but no one is offering the bandwidth or the code.

There are lots of modules available though - and usually not too hard
to track down.

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml Thanks.

--
cheers,
Ishwor Gurung


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: Steve Allgood | last post by:
I'm having trouble posting a form at the USPS web site. I've been successful using urllib at other sites, but I'm missing why this won't work: # begin code # get zip+4 import urllib def...
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...
1
by: Timothy Wu | last post by:
Hi, I'm trying to fill the form on page http://www.cbs.dtu.dk/services/TMHMM/ using urllib. There are two peculiarities. First of all, I am filling in incorrect key/value pairs in the...
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...
0
by: Alex Bryan | last post by:
I have never used the urllib class and I need to use it for an app I am working on. I am wondering if anyone has any good sites that will fill me in on it(especially the urllib.urlopen module). Or...
0
by: Simon Brunning | last post by:
2008/6/24 Alex Bryan <alexnbryan@gmail.com>: This is for urllib2: <http://www.voidspace.org.uk/python/articles/urllib2.shtml> If you must stick with urllib: ...
3
by: Martin Bachwerk | last post by:
Hello, I'm trying to load a couple of pages using the urllib2 module. The problem is that I live in Germany and some sites seem to look at the IP of the client and forward him to a localized...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.