3

Here is my server.py:

import BaseHTTPServer
import SocketServer

class TestRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        self.wfile.write("hello world at %s" % __file__)

server = BaseHTTPServer.HTTPServer(('', 10000), TestRequestHandler)
#server = SocketServer.ThreadingTCPServer(('', 8888), TestRequestHandler)
server.serve_forever()

Here is my client.py:

import urllib2
req = urllib2.Request('http://127.0.0.1:10000/')
handle = urllib2.urlopen(req)
content = handle.read()

Then I start server.py, it works.

When I start client.py, I get this error on Windows 7, Python 2.6:

Traceback (most recent call last):
  File "D:\Dropbox\Forge\urllib-error\client.py", line 3, in <module>
    handle = urllib2.urlopen(req)
  File "C:\Python26\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python26\lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "C:\Python26\lib\urllib2.py", line 409, in _open
    '_open', req)
  File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 1161, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python26\lib\urllib2.py", line 1136, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions>

When I open http://127.0.0.1:10000/ from a browser, it works. It means server is fine.

When I replace http://127.0.0.1:10000/ with http://127.0.0.1/ or http://127.0.0.1:80/ in client.py, everything works fine (this is another web server on port 80 - apache).

What do I do wrong?

UPD: Same error when I use this client2.py:

import urllib2
handle = urllib2.urlopen('http://127.0.0.1:10000/')
content = handle.read()

UPD: Problem solved. It was my firewall. When disabled, everything works. Stupid, stupid me. Thanks for reading :-)

spacediver
  • 1,432
  • 1
  • 10
  • 17
Sergey Vasilyev
  • 3,231
  • 3
  • 19
  • 29
  • Nice story after all. BTW, are to able to telnet to your server?.. Like this: `telnet localhost 10000`? – spacediver Jul 24 '11 at 09:10
  • 1
    possible duplicate of [socket.error: \[Errno 10013\] An attempt was made to access a socket in a way forbidden by its access permissions](http://stackoverflow.com/questions/2778840/socket-error-errno-10013-an-attempt-was-made-to-access-a-socket-in-a-way-forbi) – Fred Foo Jul 24 '11 at 09:46
  • 2
    @Sergey Vasilyev stackoverflow is not a forum. Instead of marking the title as `[SOLVED]`, go ahead and answer your own question and accept the answer. – phihag Jul 24 '11 at 09:58
  • @phihag, that was my humble edit, and it was approved by a couple of folks of some 10K reputation. I cannot quickly find any references about this case in the SO FAQ, could you please point these out?.. – spacediver Jul 24 '11 at 15:13
  • @phihag thank you!.. Quite clear, not easily searchable :( – spacediver Jul 24 '11 at 17:14

1 Answers1

5

The local firewall prevented the connection. When it's disabled, everything works.

phihag
  • 245,801
  • 63
  • 407
  • 443