raspberry pi 无法发出任何 url 请求,连接超时

raspberry pi can't make any url requests, connection timed out

我需要将我的传感器数据更新为 thingspeak ,所以我使用 python thingspeak 库来完成它。前两天还好好的,现在不行了,连接超时,我也试过用urllib2更新,也没用。

我的网络连接很好,我可以在 Pi 上打开网页,我可以使用 thingspeak 库和 urllib2 从我的笔记本电脑更新频道。 有人可以帮助我吗

我的 thingspeak 库代码:

node = False
channel_id = ""
write = ""
if data['MAC'] in ':ab:35':
 channel_id = "XXXXX"
 write = "XXXXXXXXXXX"
 node = True

if node:
 channel = thingspeak.Channel(id=channel_id,write_key=write)
 try :
     response=channel.update({1:data['TEMP'],2:data['VOLT'],3:data['PRES'],4:data['HUM']})
    print response
    print 'Thingspeak updated!!!'
 except :
    print "connection failed"

当我尝试 urllib2 时:

f = urllib2.urlopen(url)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
  response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
  '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
  result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1240, in https_open
  context=self._context)
  File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
  raise URLError(err)
  urllib2.URLError: <urlopen error [Errno 110] Connection timed out>

当我尝试 thingspeak 库时:

>>> channel = thingspeak.Channel(id=c,write_key=w)
>>> res = channel.update({1:50,2:30,3:70,4:20})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-armv7l/egg/thingspeak/thingspeak.py", line 116,         in update
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
    raise ConnectionError(err, request=request)

requests.exceptions.ConnectionError: ('Connection aborted.', 错误(110, 'Connection timed out'))

通过创建请求对象并将它们用作 urlopen 的参数来添加新的用户代理:

import urllib2

request = urllib2.Request('http://www.example.com/')
request.add_header('User-agent', 'Mozilla/5.0 (Linux i686)')

response = urllib2.urlopen(request)

我正在使用 raspberry-pi 向 thingspeak 发送数据。 我所做的是这样的:

import httplib
import urllib

    #Preparing to send the information to the cloud
            params=urllib.urlencode({'field1':liters,'key':key})
            headers={"Content-typZZe": "application/x-www-form-urlencoded","Accept":"text/plain"}
            conn=httplib.HTTPConnection("api.thingspeak.com:80")

    #Sending the data to the thingspeak platform
            try:
                    conn.request("POST", "/update", params, headers)
                    response=conn.getresponse()
                    print response.status, response.reason
                    data=response.read()
                    conn.close()
            except:
                    print "connection failed"

而且效果很好。 密钥是您频道密钥的字符串。

希望对您有所帮助。

如果我使用 SSH 并尝试 运行 脚本,它会起作用。