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

时间:2017-05-10 04:17:53

标签: python raspberry-pi python-requests

我尝试在一个专用的Raspberry Pi(我称之为PiServer)中,每隔一秒从Raspberry Pi生成随机信号到本地WebServer。

然后,PiServer会将按摩发送回Raspberry Pi,并将数据存储在MySQL数据库中。

现在我想知道增加包发送次数需要多长时间。所以我多次运行以下代码(coffeeMaker.py)。

这是我的代码

coffeeMaker.py包含在客户端Raspberry 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;
}
?>

问题是当我将coffeeMaker.py的数量增加到100(同时运行100个文件)时,某些连接已被删除,并且日志显示如下

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',))

我想知道问题是否因WebServer(PHP文件)或带宽受限或PiServer资源而发生。

注意:我曾尝试将Amazon EC2用作服务器以及笔记本电脑和PiServer。所有人都有同样的问题。

非常感谢。

0 个答案:

没有答案