AttributeError: module 'urllib3' has no attribute 'urlopen' in python

AttributeError: module 'urllib3' has no attribute 'urlopen' in python

我正在尝试将温度数据发送到我当前在线的网站之一。此代码包括通过传感器 (ds18b20) 测量温度,将该数据发送到名为 temp_pi 的 mysql 数据库,特别是发送到名为 TAB_CLASSROOM 的 table 数据库,最后发送该数据数据到我的网页上。除了 sendDataToServer() 部分之外,此代码中的所有内容都会运行。我在此特定行之前指定了错误。我在我的网站上设置了 PHP 以使其正常工作。

import os
import glob
import time
import MySQLdb
import datetime
import mysql.connector
from mysql.connector import Error

#define db and cur

db = MySQLdb.connect(host = "127.0.0.1", user = "root", passwd = "xB7O4fXmuMpF6M0u", db = "temp_pi")
cur = db.cursor()

#connection to the database
try:
    connection = mysql.connector.connect(host='127.0.0.1',
                             database='temp_pi',
                             user='root',
                             password='xB7O4fXmuMpF6M0u')

    if connection.is_connected():
       db_Info = connection.get_server_info()
       print("Connected to MySQL database... MySQL Server version on ",db_Info)
       cursor = connection.cursor()
       cursor.execute("select database();")
       record = cursor.fetchone()
       print ("Your connected to - ", record)

except Error as e :
    print ("Error while connecting to MySQL", e)

#obtaining the temperature through the ds18b20 sensor            
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c  
#Defining sendDataToServer() and trying to send this data towards my website
def sendDataToServer():
    global temperature

    threading.Timer(600,sendDataToServer).start()
    print("Mesuring...")
    read_temp()
    temperature = read_temp()
    print(temperature)
    temp= read_temp()
    urllib3.urlopen("http://francoouesttemp.tech/weather/add_data.php?temp="+temp).read()
#insertion of data into the mysql database
while True:
        print("putting temperature data into temp_pi database")
        i = datetime.datetime.now()
        year = str(i.year)
        month = str(i.month)
        day = str(i.day)
        date = day + "-" + month + "-" + year

        hour = str(i.hour)
        minute = str(i.minute)
        second = str(i.second)
        timestr = hour + ":" + minute + ":" + second

        valT = str(read_temp())

        try:
            cur.execute("""INSERT INTO TAB_CLASSROOM(temp_c,T_Date,T_Time) VALUES(%s,%s,%s)""",(valT,i,timestr))
            db.commit()
        except:
            db.rollback()

        time.sleep(5)

        #this is the part where my code tells me : NameError : name 'urllib3' is not defined ----- I want this part of the code to send the temperature, date and time over to my website.     
        sendDataToServer()

cur.close()  
db.close()

如果您想使用 urllib3 发送请求,您需要先 create a pool manager

或者,您可以使用 Python 标准库中的 HTTP 客户端。它的 urlopen 功能被称为 urllib.request.urlopen. Depending on what you are trying to do, the requests package 也可能是一个选项,但它在 HTTPS URL 的证书管理方面有一定的缺点(内置客户端将自动使用系统证书存储)。

import urllib 
import requests
url = '....'
response = urllib.request.urlopen(url)