【python】A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Error as follows:

Traceback (most recent call last):
  File "", line 1, in
    photos = flickr.photos_search(lat='50.21143279', lon='-5.480750799', radius='1')
  File "D:\Program Files (x86)\Python27\lib\site-packages\flickrapi\__init__.py", line 349, in handler
    parse_format=args['format'], **args)
  File "D:\Program Files (x86)\Python27\lib\site-packages\flickrapi\__init__.py", line 427, in __wrap_in_parser
    data = wrapped_method(*args, **kwargs)
  File "D:\Program Files (x86)\Python27\lib\site-packages\flickrapi\__init__.py", line 402, in __flickr_call
    flicksocket = urllib2.urlopen(url, post_data)
  File "D:\Program Files (x86)\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "D:\Program Files (x86)\Python27\lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "D:\Program Files (x86)\Python27\lib\urllib2.py", line 409, in _open
    '_open', req)
  File "D:\Program Files (x86)\Python27\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "D:\Program Files (x86)\Python27\lib\urllib2.py", line 1173, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "D:\Program Files (x86)\Python27\lib\urllib2.py", line 1148, in do_open
    raise URLError(err)
URLError:

1. netstat
这篇《PROXY SERVER 代理服务器》作为基础知识,外加这篇端口用途描述,我们可以使用命令netstat -ano来查看本机端口状态,通过端口(如www默认端口80,FTP默认端口21,代理服务器端口8080,邮件服务端口25 or 110等。记住端口也有利于查杀木马病毒,如139端口)其中参数意义如下:

命令格式:
netstat [-r] [-s] [-n] [-a]
参数含义:
-r 显示本机路由表的内容;
-s 显示每个协议的使用状态(包括TCP协议、UDP协议、IP协议);
-n 以数字表格形式显示地址和端口;
-a 显示所有主机的端口号。

2. fix the problem
import urllib
URL="http://66.102.11.99" # http://www.google.com
f = urllib.urlopen(URL)
f.read()

若read成功则是DNS问题,check DNS命令:nslookup http://www.google.com

3. get the proxy server info
import urllib
urllib.getproxies()

注:getproxies() does not work with dynamic proxy configuration, like when using PAC(Proxy Auto Configuration- WiKi)

Once you know info about your proxy server, you can build the connection in Python like this:
import urllib2
proxy_handler = urllib2.ProxyHandler({'http': 'http://yourcorporateproxy:12345/'})
proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = build_opener(proxy_handler, proxy_auth_handler)
opener.open(‘http://www.stackoverflow.com’)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: