java
  • python
  • raspberry-pi3
  • 2017-03-18 123 views 0 likes 
    0

    使用urllib3時,我試圖將數據從python腳本發送到位於本地服務器(localhost8080)上的Java servlet。我收到以下錯誤:在本地主機上使用請求或urllib3從python請求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

    回溯告訴你什麼是問題:'拒絕連接'。您的計算機的端口8080上是否正在偵聽? – leovp

    +0

    (在Ubuntu上)netstat -ntlp | grep 8080應該列出你的程序是端口正在監聽 –

    回答

    0

    我會建議使用python-請求庫:https://pypi.python.org/pypi/requests

    這是簡單得多的urllib使用。

    +0

    爲什麼更簡單?你能給你的答案增加一個例子和解釋嗎? – xlm

    +0

    感謝所有的答覆。 –

    相關問題