3
我是Python新手,根本沒有編碼背景。 我正在嘗試使用我的Raspberry Pi向Google Analytics發送數據。問題是Google Analytics測量協議使用HTTP S - 我的代碼不適用。urllib2打開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指南是否有幫助。
我已經在網上閱讀了很多關於如何解決這個問題的文章,但是因爲我對所有這些都是陌生的,所以沒有讓人困惑。
如果有人能給我一隻手,或者指導我如何工作的初學者指南,我將非常感激!
您是否嘗試將'https://'更改爲'http://'。它應該繞過嘗試使用urllib2創建加密(ssl)連接的障礙。 – Matt