Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法建立新连接:[WinError 10060]_Python_Raspberry Pi_Python Requests - Fatal编程技术网

Python 无法建立新连接:[WinError 10060]

Python 无法建立新连接:[WinError 10060],python,raspberry-pi,python-requests,Python,Raspberry Pi,Python Requests,我试图在一个专用的Raspberry Pi(我称之为PiServer)中,每随机秒生成一个从Raspberry Pi到本地Web服务器的随机信号 然后,PiServer将消息发送回Raspberry Pi,并将数据存储在MySQL数据库中 现在我想看看增加包裹发送数量需要多少时间。因此,我多次运行以下代码(coffeeMaker.py) 这是我的密码 coffeeMaker.py包含在客户端树莓Pi中 from time import sleep import requests import d

我试图在一个专用的Raspberry Pi(我称之为PiServer)中,每随机秒生成一个从Raspberry Pi到本地Web服务器的随机信号

然后,PiServer将消息发送回Raspberry Pi,并将数据存储在MySQL数据库中

现在我想看看增加包裹发送数量需要多少时间。因此,我多次运行以下代码(coffeeMaker.py)

这是我的密码

coffeeMaker.py包含在客户端树莓Pi中

from time import sleep
import requests
import datetime
import random

home = "A"
device = ["CoffeeModel01", "WashingMachine01", "RiceCooker01", "DryingMachine01", "MicrowaveModel01"]

while True:
    num1 = random.randint(0, 1)
    num2 = random.randint(0, 1)
    num3 = random.randint(0, 1)
    deviceNum = random.randint(0,4)
    code = "%d%d%d" % (num1, num2, num3)
    interval = random.uniform(2.0, 4.0)
    timestamp = datetime.datetime.now()
    payload = {"Device": device[deviceNum], "Code": code, "Time": format(timestamp), "Home": home}
    print "Data Sended : Device = " + device[deviceNum] + ", Code = " + code
    r = requests.post("http://160.18.2.109/HomeAuto.php", data=payload)
    if r.status_code != 200:
        raise Exception("Failed to post data to server")
    print(r.text)
    sleep(interval)
HomeAuto.php包含在PiServer中并与SQL一起使用

<?php include "dbinfo.inc";

  /* Connect to MySQL and select the database. */
  $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);

  if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();

  $database = mysqli_select_db($connection, DB_DATABASE); 

  /* Get file from Raspberry Pi */
  $device = $_POST['Device'];
  $home = $_POST['Home'];
  $time = $_POST['Time'];
  $code = $_POST['Code'];

  /* Ensure that the Home table exists. */
  VerifyHomeTable($connection, DB_DATABASE, $home);

  $device = mysqli_real_escape_string($connection, $device);
  $home = mysqli_real_escape_string($connection, $home);
  $time = mysqli_real_escape_string($connection, $time);
  $code = mysqli_real_escape_string($connection, $code);

  if (strlen($device) || strlen($home) || strlen($time) || strlen($code)) {
    AddToLog($connection, $device, $home, $time, $code);
    AddToHome($connection, $device, $home, $time, $code);
  }

  $result = mysqli_query($connection, "SELECT * FROM $device Where Code=$code"); 

   while($query_data = mysqli_fetch_row($result)) {
       echo "Message : " . $query_data[1];
   }

/* Add an data to the AllLog table. */
function AddToLog($connection, $device, $home, $time, $code) {
   $query = "INSERT INTO `MainLog` (`Device`, `Code`, `Time`, `Home`) VALUES ('$device','$code','$time', '$home');";

   if(!mysqli_query($connection, $query)) echo "Error adding log data.";
}

/* Add an data to the Home table. */
function AddToHome($connection, $device, $home, $time, $code) {
   $query = "INSERT INTO `$home` (`Device`, `Code`, `Time`) VALUES ('$device','$code','$time');";

   if(!mysqli_query($connection, $query)) echo "Error adding log data.";
}

/* Check whether the table exists and, if not, create it. */
function VerifyHomeTable($connection, $dbName, $home) {
  if(!TableExists($home, $connection, $dbName)) { 
     $query = "CREATE TABLE `$home` (
         `ID` int(11) NOT NULL AUTO_INCREMENT,
         `Device` varchar(45) DEFAULT NULL,
         `Code` varchar(10) DEFAULT NULL,
         `Time` varchar(90) DEFAULT NULL,
         PRIMARY KEY (`ID`),
         UNIQUE KEY `ID_UNIQUE` (`ID`)
       ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8";

     if(!mysqli_query($connection, $query)) echo "Error creating table.";
  }
}

/* Check for the existence of a table. */
function TableExists($tableName, $connection, $dbName) {
  $t = mysqli_real_escape_string($connection, $tableName);
  $d = mysqli_real_escape_string($connection, $dbName);

  $checktable = mysqli_query($connection, 
      "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");

  if(mysqli_num_rows($checktable) > 0) return true;

  return false;
}
?>

看起来像是服务器问题。尝试减少请求数,或者在
请求中使用try-except块。post
我已经尝试使用try-except句柄了。但问题是,无论如何都要配置服务器端以接收比这更多的请求吗?
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='160.18.2.109', port=80): Max retries exceeded with url: /HomeAuto.php (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x03EEB6D0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))