在本地主机上使用请求或 urllib3 从 python 请求 servlet 时出错



使用 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

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

它比urllib更容易使用。

最新更新