473,666 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysteries of urllib/urllib2

I'm trying to use urllib2 to download a page (I'd rather use urllib,
but I need to change the User-Agent header to look like a browser or
G**gle won't send it to me, the big meanies). The following (pinched
from Dive Into Python) seems to work perfectly in Idle, but falls at
the final hurdle when run as a cgi script - can anyone suggest
anything I may have overlooked?

request = urllib2.Request (some_URL)
request.add_hea der('User-Agent', 'some_plausible _string')
opener = urllib2.build_o pener()
data = opener.open(req uest).read()

Jul 3 '07 #1
5 1739
On Jul 3, 9:43 am, Adrian Smith <adrian_p_sm... @yahoo.comwrote :
The following (pinched
from Dive Into Python) seems to work perfectly in Idle, but falls at
the final hurdle when run as a cgi script - can anyone suggest
anything I may have overlooked?

request = urllib2.Request (some_URL)
request.add_hea der('User-Agent', 'some_plausible _string')
opener = urllib2.build_o pener()
data = opener.open(req uest).read()
Most likely the account that cgi script is running as does not have
permissions to access the net. Check the traceback to be sure. Put
this at the top of your cgi script:

import cgitb; cgitb.enable()

--Ben

Jul 3 '07 #2
On Jul 3, 11:25 pm, Ben Cartwright <benc.nos...@gm ail.comwrote:
On Jul 3, 9:43 am, Adrian Smith <adrian_p_sm... @yahoo.comwrote :
The following (pinched
from Dive Into Python) seems to work perfectly in Idle, but
falls at the final hurdle when run as a cgi script - can
anyone suggest anything I may have overlooked?
request = urllib2.Request (some_URL)
request.add_hea der('User-Agent', 'some_plausible _string')
opener = urllib2.build_o pener()
data = opener.open(req uest).read()

Most likely the account that cgi script is running as does not
have permissions to access the net. Check the traceback to be
sure. Put this at the top of your cgi script:

import cgitb; cgitb.enable()
Well, it worked with urllib (resulting in a G**gle 403 your-client-
does-not-have-permission-to-get-urlX page), so I think it must have
some access. Apparently there's a way to change the user-agent string
by subclassing urllib's URLopener class, but that's beyond my comfort
zone at present.

Jul 3 '07 #3
On Jul 3, 11:14 am, Adrian Smith <adrian_p_sm... @yahoo.comwrote :
The following (pinched
from Dive Into Python) seems to work perfectly in Idle, but
falls at the final hurdle when run as a cgi script
Put this at the top of your cgi script:
import cgitb; cgitb.enable()
Did you even try this? Asking for Python help without posting the
traceback is like phoning your mechanic and saying, "My car is making
a generic rattling noise, can you tell me what the problem is without
looking under the hood?"
Apparently there's a way to change the user-agent string
by subclassing urllib's URLopener class, but that's beyond my comfort
zone at present.
Untested:

import urllib
url = 'http://groups.google.c om/group/Google-AJAX-Search-API/
browse_thread/thread/a0eb87ad13b1176 2'
opener = urllib.FancyURL opener()
opener.addheade rs = [('User-Agent', 'Fauxzilla 4.0')]
data = opener.open(url ).read()

Hope that helps,
--Ben

Jul 3 '07 #4
Adrian Smith wrote:
I'm trying to use urllib2 to download a page (I'd rather use urllib,
but I need to change the User-Agent header to look like a browser or
G**gle won't send it to me, the big meanies). The following (pinched
from Dive Into Python) seems to work perfectly in Idle, but falls at
the final hurdle when run as a cgi script - can anyone suggest
anything I may have overlooked?

request = urllib2.Request (some_URL)
request.add_hea der('User-Agent', 'some_plausible _string')
opener = urllib2.build_o pener()
data = opener.open(req uest).read()
I doubt that's the problem here, but don't use a USER-AGENT string
that ends in "m" without a preceding "m" when the USER-AGENT
string is the last element of the header. Coyote Point load balancers
will drop the packet.

(Coyote Point uses regular expressions to parse HTTP headers, and
I think somebody wrote "\m" where they meant "\n".)

John Nagle
Jul 3 '07 #5
On Jul 4, 12:42 am, Ben Cartwright <benc.nos...@gm ail.comwrote:
On Jul 3, 11:14 am, Adrian Smith <adrian_p_sm... @yahoo.comwrote :
The following (pinched
from Dive Into Python) seems to work perfectly in Idle, but
falls at the final hurdle when run as a cgi script
Put this at the top of your cgi script:
import cgitb; cgitb.enable()

Did you even try this? Asking for Python help without posting the
traceback is like phoning your mechanic and saying, "My car is
making a generic rattling noise, can you tell me what the problem
is without looking under the hood?"
Sorry, I thought as the cgi did appear to have web access it wasn't
applicable, and it's amazing what some mechanics can infer from engine
noise. cgitb certainly does send back an impressive amount of
information, I'll be sure to use it in future.
Apparently there's a way to change the user-agent string
by subclassing urllib's URLopener class, but that's beyond my
comfort zone at present.

Untested:

import urllib
url = 'http://groups.google.c om/group/Google-AJAX-Search-API/
browse_thread/thread/a0eb87ad13b1176 2'
opener = urllib.FancyURL opener()
opener.addheade rs = [('User-Agent', 'Fauxzilla 4.0')]
data = opener.open(url ).read()
That works a treat, thanks!

Jul 3 '07 #6

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

Similar topics

4
5181
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 files. Or at least, it's not clear from the description of socket.ssl what it does if they're omitted. However, urlopen has no way to specify such things. Nor should it - for typical uses, a person simply trying to retrieve data from an ssl site...
1
4100
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
2034
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 user/password-request. So exists a possibility that I could cancel all site requests with user/password-dialogues? Thx
0
3583
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 my cry for help. The server is an elog logbook server (http://midas.psi.ch/elog/). It is protected with a password and an empty username. I can login both using urllib and urllib2 (suppose the password is "foobar", the logbook is running on port...
11
3562
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 of the base url and grab all the urls from "href" property of "a" tag 4. Call filter to validate every url grabbed 5. Continue 3-4 until the number of url grabbed gets the limit
0
2442
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 http_proxy=http://user:passwd@host:port/ and https_proxy=$http_proxy i get a traceback : Traceback (most recent call last): File "test_urllib.py", line 2, in ? urllib.urlopen("https://sf.net/")
5
7686
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 open the connection within 15 seconds, urllib times out. But if the host end opens the connection, then never sends anything, urllib waits for many minutes before timing out. Any idea how to deal with this? And don't just say "use urllib2"...
6
2068
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 anything, since it's just an internal optimisation from one point of view -- but it's also what the RFC (2616, section 10.3.2, first para) says SHOULD happen. I am trying to understand, what does it mean. Should the original url be avaiable to...
0
3696
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 combination: resp = opener.open('http://www.amm.com/login.asp') theurl = 'http://www.amm.com/login.asp' body={'username':'AMMT54590570','password':'AMMT32 564288'} #!/usr/bin/env python # -*- coding: UTF-8 -*-
0
8363
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8787
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8561
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.