我是 Python 新手,完全没有编码背景。 我正在尝试使用我的 Raspberry Pi 将数据发送到 Google Analytics。问题是 Google Analytics Measurement Protocol 使用 HTTPS - 我的代码无法使用它。
所以我现在的代码是这样的
import urllib2
import time
import RPi.GPIO as io
io.setmode(io.BCM)
door_sensor = 18
sensorTrigger = True
io.setup(door_sensor, io.IN, pull_up_down=io.PUD_UP)
# function for the door opening
def door_open():
print("Door Open")
urllib2.urlopen("https://www.google-analytics.com/collect?v=1&tid=UA- 3458xxxx-1&cid=555&t=event&ec=doors&ea=open&el=office").close
# function for the door closing
def door_close():
print("Door Close")
while True:
if io.input(door_sensor): # if door is opened
if (sensorTrigger):
door_open() # fire GA code
sensorTrigger = False # make sure it doesn't fire again
if not io.input(door_sensor): # if door is closed
if not (sensorTrigger):
door_close() # fire GA code
sensorTrigger = True # make sure it doesn't fire again
我不断收到的错误是...
Traceback (most recent call last):
File "/home/pi/Desktop/GADoorSensor.py", line 23, in <module>
door_open() # fire GA code
File "/home/pi/Desktop/GADoorSensor.py", line 14, in door_open
urllib2.urlopen("https://www.google-analytics.com/collect?v=1&tid=UA-3458xxxx-1&cid=555&t=event&ec=doors&ea=open&el=office").close
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 [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>
我关注了THIS指导是否有帮助。
我已经在网上阅读了很多关于如何解决这个问题的文章,但是因为我对这一切都是陌生的,所以它不会让人感到困惑。
如果有人可以帮助我或指导我找到有关如何解决此问题的初学者指南,我将不胜感激!
最佳答案
我在 http.client 上运气更好,因此......
import http.client
myDestination = "https://www.google-analytics.com/collect?v=1&tid=UA- 3458xxxx-1&cid=555&t=event&ec=doors&ea=open&el=office"
conn = http.client.HTTPSConnection(myDestination)
然后使用...
conn.request()
和
conn.response()
不要忘记...
conn.close()
关于python - urllib2 开启HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41756742/