473,396 Members | 2,020 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.

Can not get urllib.urlopen to work

I am trying to implement the recipe listed at
http://aspn.activestate.com/ASPN/Coo.../Recipe/211886

However, I can not get to first base. When I try to run

import urllib
fo=urllib.urlopen("http://www.dictionary.com/")
page = fo.read()

I get:

Traceback (most recent call last):
File "C:/Program Files/Python/Lib/idlelib/testurl", line 2, in -toplevel-
fo=urllib.urlopen("http://www.dictionary.com/")
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 76, in urlopen
return opener.open(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 181, in open
return getattr(self, name)(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 297, in open_http
h.endheaders()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 712, in endheaders
self._send_output()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 597, in _send_output
self.send(msg)
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 564, in send
self.connect()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 548, in connect
raise socket.error, msg
IOError: [Errno socket error] (10061, 'Connection refused')
Jul 18 '05 #1
11 5014
More information:

I am running Python 2.3.4, Windows 2000 Pro, along with Norton Internet
Security Professional. I do not get any messages from Norton.
Jul 18 '05 #2
Pater Maximus wrote:
I am trying to implement the recipe listed at
http://aspn.activestate.com/ASPN/Coo.../Recipe/211886

However, I can not get to first base. When I try to run

import urllib
fo=urllib.urlopen("http://www.dictionary.com/")
page = fo.read()

I get:
Traceback (most recent call last):
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 548, in connect
raise socket.error, msg
IOError: [Errno socket error] (10061, 'Connection refused')


Do you have any reason to think that this site is currently
hosting a web server?

Attempting to connect with IE or Telnet on port 80 results in
a similar response.

(Telnet is always a good thing to try, before posting about
server problems...)

-Peter
Jul 18 '05 #3

"Pater Maximus" <em***@home.com> wrote in message
news:3J********************@comcast.com...
IOError: [Errno socket error] (10061, 'Connection refused')


My browser says the same thing. ;-)
Alan Isaac
Jul 18 '05 #4
Pater Maximus wrote:
I am trying to implement the recipe listed at
http://aspn.activestate.com/ASPN/Coo.../Recipe/211886

However, I can not get to first base. When I try to run

import urllib
fo=urllib.urlopen("http://www.dictionary.com/")
page = fo.read()


I can't even connect to it with my web browser. Can you?
If you can, they are probably checking the user-agent sent
by urllib, to make it harder to do this sort of automated
screen scraping.

See the docs at
http://www.python.org/doc/current/li...le-urllib.html

for an example of how to change the default user-agent.

Here's one for MSIE under Win2K

Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)

Andrew
da***@dalkescientific.com
Jul 18 '05 #5
>I am trying to implement the recipe listed at
http://aspn.activestate.com/ASPN/Coo.../Recipe/211886

However, I can not get to first base. When I try to run

import urllib
fo=urllib.urlopen("http://www.dictionary.com/")
page = fo.read()

I get:

Traceback (most recent call last):
File "C:/Program Files/Python/Lib/idlelib/testurl", line 2, in -toplevel-
fo=urllib.urlopen("http://www.dictionary.com/")
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 76, in urlopen
return opener.open(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 181, in open
return getattr(self, name)(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 297, in open_http
h.endheaders()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 712, in endheaders
self._send_output()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 597, in _send_output
self.send(msg)
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 564, in send
self.connect()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 548, in connect
raise socket.error, msg
IOError: [Errno socket error] (10061, 'Connection refused')


Connection refused is the key. Apparently dictionary.com figured out that
people were trying to use their resourses without giving them credit. I
have done some research on how to accomplish this so that people can not use
the cgi-bin programs (and others) that I write.

A few years back I needed to get information about the weather based on zip
code. I used to use weather.com but they fixed the hole. They made any
port 80 request forward to another address, which in turn forwarded you to
the original request. So if you west to www.weather.com, it would forward
you to www2.weather.com, then back to www.weather.com. The index page at
www.weather.com would then check the referrer to see if it came from
www2.weather.com. If the referrer was correct, then you got the page
content. If it was wrong, there was no page.

Similarly, www2.weather.com would check to see that the referrer was
www.weather.com... so there was no way around it.

I use another method to protect my programs... but it still does the same
thing ultimatly... stops people from using my programs.

Like it has been mentioned, a good starting place is Telnet. I tried
telnetting when I first read this post and got a connection refused, but now
I can get through - weird. I also tried a source dump from lynx... which 10
minutes ago did not work, but now it does.

# telnet www.dictionary.com 80

# lynx -source -preparse -dump http://www.dictionary.com

These both work to show the source of the index page. And the python recipe
even works now.... go figure.
Jul 18 '05 #6
By the way, this does not work as well as it could.

It only lists the definitions if they are presented in listed <LI> tags.

So for the word greedy... it works. But, for the word greed, it does not.

I am sure some better parsing of the pages could be done for those
definitions lot listed in li tags.

Maybe I will play around with it and see what I can come up with.
Jul 18 '05 #7

Sean Berry wrote:
A few years back I needed to get information about the weather based on zip code. I used to use weather.com but they fixed the hole. They made any
port 80 request forward to another address, which in turn forwarded you to
the original request. So if you west to www.weather.com, it would forward
you to www2.weather.com, then back to www.weather.com. The index page at
www.weather.com would then check the referrer to see if it came from
www2.weather.com. If the referrer was correct, then you got the page
content. If it was wrong, there was no page.

Similarly, www2.weather.com would check to see that the referrer was
www.weather.com... so there was no way around it.
Au contraire, monsieur... there is no reason I can't set the referrer
header (using httplib).
I use another method to protect my programs... but it still does the same
thing ultimatly... stops people from using my programs.


It is a level of discouragement, and may be sufficient to stop the
simplest of abuses, but the reality is that, given enough work, one can have
their Python program recreate any HTTP transaction or series of transactions
needed. If you want to redirect me to a dynamically generated URL, fine...
I'll read the Location header and go there, if you want to set a cookie
there, fine, I'll read the Set-Cookie header, set it, then return it to you,
just as a browser does. If you want to check the User-Agent, I can send
headers that look exactly like what MSIE or any other browser would send.
You can make it difficult, no doubt, and doing so may be a wise thing to do,
but it is wrong to say that just becasue A sends you to B, and B refers you
back to A, there is no way around it.

I am currently doing exactly this sort of thing, but not to abuse
others' work. We pay for a service where data is published on a web site. We
need to get at this data several times an hour, 24X7. There is a login page,
and one or more HTML forms that must be filled out and submitted, and
cookies set and checked to get to the pages that contain the data we need.
It was not designed to be machine read, but that doesn't mean reading it in
an automated manner is "wrong" or abusive - we're paying for that data, they
just happen to present it in a format that's not all that conducive to
automated machine parsing. Fine, I'll do the extra work to get at it in that
manner.

I suppose you could meter the number of requests that a particular IP is
making or something on the server side, and there isn't much that can be
done on the requester's end. With access to several machines, though, even
this hurdle could be cleared.

So, anyway, if the OP wants to get more sophisticated about automated
surfing, check out the httplib module:
http://docs.python.org/lib/module-httplib.html

FYI... www.dictionary.com loads (for me, now) through a simple telnet
request:

ej@sand:~/src/python> telnet www.dictionary.com 80
Trying 66.161.12.81...
Connected to www.dictionary.com.
Escape character is '^]'.
GET / HTTP/1.0

HTTP/1.1 200 OK
Date: Wed, 27 Oct 2004 22:57:49 GMT
Server: Apache
Cache-Control: must-revalidate
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Wed, 27 Oct 2004 22:58:07 GMT
Pragma: no-cache
Connection: close
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>

<snip!>

HTH,
-ej
Jul 18 '05 #8
> Au contraire, monsieur... there is no reason I can't set the referrer
header (using httplib).


I stand corrected.

Jul 18 '05 #9
Pater Maximus wrote:
I am trying to implement the recipe listed at
http://aspn.activestate.com/ASPN/Coo.../Recipe/211886

However, I can not get to first base. When I try to run

import urllib
fo=urllib.urlopen("http://www.dictionary.com/")
page = fo.read()

I get:

Traceback (most recent call last):
File "C:/Program Files/Python/Lib/idlelib/testurl", line 2, in -toplevel-
fo=urllib.urlopen("http://www.dictionary.com/")
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 76, in urlopen
return opener.open(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 181, in open
return getattr(self, name)(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 297, in open_http
h.endheaders()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 712, in endheaders
self._send_output()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 597, in _send_output
self.send(msg)
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 564, in send
self.connect()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 548, in connect
raise socket.error, msg
IOError: [Errno socket error] (10061, 'Connection refused')

Suspect the action of a firewall or just assume you were unlucky and the
server was down when you hit it. I had no problem just now:

$ python
Python 2.3.4 (#1, Jun 13 2004, 11:21:03)
[GCC 3.3.1 (cygming special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
import urllib
fo=urllib.urlopen("http://www.dictionary.com/")
page = fo.read()


regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #10
Problem solved!!

First, thanks to all who responded.

It turns out the villain was "Norton Personal Firewall". Since it did not
warn me that it was blocking an attempt to reach the internet, I assumed it
was not the problem.

Bad assumption.

Everything worked when I configured Norton to allow pythonw.exe to contact
the internet.
"Pater Maximus" <em***@home.com> wrote in message
news:3J********************@comcast.com...
I am trying to implement the recipe listed at
http://aspn.activestate.com/ASPN/Coo.../Recipe/211886

However, I can not get to first base. When I try to run

import urllib
fo=urllib.urlopen("http://www.dictionary.com/")
page = fo.read()

I get:

Traceback (most recent call last):
File "C:/Program Files/Python/Lib/idlelib/testurl", line 2, in -toplevel- fo=urllib.urlopen("http://www.dictionary.com/")
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 76, in urlopen
return opener.open(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 181, in open
return getattr(self, name)(url)
File "C:\PROGRA~1\PYTHON\lib\urllib.py", line 297, in open_http
h.endheaders()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 712, in endheaders
self._send_output()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 597, in _send_output
self.send(msg)
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 564, in send
self.connect()
File "C:\PROGRA~1\PYTHON\lib\httplib.py", line 548, in connect
raise socket.error, msg
IOError: [Errno socket error] (10061, 'Connection refused')

Jul 18 '05 #11
Pater Maximus wrote:
Problem solved!!

First, thanks to all who responded.

It turns out the villain was "Norton Personal Firewall". Since it did not
warn me that it was blocking an attempt to reach the internet, I assumed it
was not the problem.

Bad assumption.

Everything worked when I configured Norton to allow pythonw.exe to contact
the internet.


While that might have been *another* of your problems,
there definitely was a problem connecting to that host
at the time you posted.

The most difficult problems to troubleshoot are those that
involve more than one cause. I can see how this one would
have been a real bitch to find. :-)

-Peter
Jul 18 '05 #12

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...
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...
0
by: Chris | last post by:
hello, I have an odd behaviour. I try to download some files connected to a specific webpage (e.g. all stylesheets) with urllib2.urlopen(x) This seems to hang on the 2nd file or so. Doing the...
4
by: william | last post by:
I've got a strange problem on windows (not very familiar with that OS). I can ping a host, but cannot get it via urllib (see here under). I can even telnet the host on port 80. Thus network...
6
by: JabaPyth | last post by:
Hello, I'm trying to use the urllib module, but when i try urllib.urlopen, it gives me a socket error: >>import urllib >>print urllib.urlopen('http://www.google.com/').read() Traceback (most...
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...
4
by: kgrafals | last post by:
Hi, I'm just trying to read from a webpage with urllib but I'm getting IOErrors. This is my code: import urllib sock = urllib.urlopen("http://www.google.com/") and this is the error:
0
by: gmguyx | last post by:
I tried using urllib.urlopen to open a personalized webpage (my.yahoo.com) but it doesn't work: print urllib.urlopen(http://my.yahoo.com).read() Instead of returning my.yahoo.com, it returns a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
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.