python - urllib2 开启HTTPS

标签 python google-analytics raspberry-pi

我是 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/

相关文章:

raspberry-pi - 树莓派 pjsip 编译错误 : emmintrin. h 没有这样的文件或目录

android - 如何在我的 Android 应用程序(如 SL4A)中集成 Python 脚本

python - 从字符串中获取所有可能的英文单词

swift - Swift 中的自定义维度

javascript - 为什么 Google Analytics 内容实验的结果没有显示?

node.js - 使用 Raspbian 升级 Node JS 版本在 Raspberry Pi 4 中不起作用

Python,当键是科学格式的 float 时,如何按键对字典进行排序?

python - 类型错误 : 'float' object is not iterable

android - 格式化异常消息以包含 Google Analytics for Android 中的整个堆栈跟踪?

assembly - 使用树莓派学习汇编语言?