使用请求或在localhost上的urllib3从python请求servlet时出错

时间:2017-03-18 08:52:52

标签: java python raspberry-pi3

使用urllib3,我试图将数据从python脚本发送到位于我本地服务器(localhost8080)上的Java servlet。我收到以下错误:

Traceback (most recent call last):
  File "utlTest4.py", line 8, in <module>
    r = http.request('GET', url='http://localhost:8080/raspberryPi/GetValue')
  File "/usr/lib/python2.7/dist-packages/urllib3/request.py", line 68, in reques                                                                                        t
    **urlopen_kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/request.py", line 81, in reques                                                                                        t_encode_url
    return self.urlopen(method, url, **urlopen_kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 153, in u                                                                                        rlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 579, i                                                                                        n urlopen
    release_conn=release_conn, **response_kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 579, i                                                                                        n urlopen
    release_conn=release_conn, **response_kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 579, i                                                                                        n urlopen
    release_conn=release_conn, **response_kw)
  File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 559, i                                                                                        n urlopen
    _pool=self, _stacktrace=stacktrace)
  File "/usr/lib/python2.7/dist-packages/urllib3/util/retry.py", line 265, in in                                                                                        crement
    raise MaxRetryError(_pool, url, error)
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=8080                                                                                        ): Max retries exceeded with url: /raspberryPi/GetValue (Caused by ProtocolError                                                                                        
('Connection aborted.', error(111, 'Connection refused')))

我的代码是:

import urllib3 import requests

if name == "main": #quote_number = 1

http = urllib3.PoolManager()
r = http.request('GET',url='http://localhost:8080/raspberryPi/GetValue')

if r.status != 200:
raise Exception('ServerError', quote_number, r.status)

print r

1 个答案:

答案 0 :(得分:0)

我建议使用python-requests库:https://pypi.python.org/pypi/requests

使用起来比urllib更简单。