ich möchte Sensordaten, die ich als Python dict vorliegen habe, in ein Json Object parsen und per Requests.post an einen Webservice senden.
Das Problem ist das egal was ich per POST senden will benutzt er GET und wenn er doch mal POST benutzt ist das Packet aber leer.
Mein PHP Code vom Webservice
Code: Alles auswählen
<?php
include_once("../constants.php");
if ($_POST)
{
$jsonurl = $_POST;
} else {
$jsonurl = $_REQUEST['q'];
}
if ( !empty( $jsonurl ) )
{
$jsonresult = json_decode( $jsonurl, true );
if ( $jsonresult[0] == '140536' || $jsonresult['s']['1'] == '140536' )
{
mysql_connect(DB_SERVER,DB_USER,DB_PASS); //database
mysql_select_db(DB_NAME);
$search=array("\\","\0","\n","\r","\x1a","'",'"');
$replace=array("\\\\","\\0","\\n","\\r","\Z","\'",'\"');
$clean_string = str_replace($search,$replace,$jsonurl);
$time = time();
$remoteip = $_SERVER['REMOTE_ADDR'];
// Adding a field to the db_traffic.tbl in the database, where the info is logged
$sql = "INSERT INTO ".TBL_DB_TRAFFIC." VALUES ('0', '$remoteip', FROM_UNIXTIME($time), '$clean_string', 'WS LOG')";
$result = mysql_query($sql);
$ping = array( 's'=>1, 'u'=>0 );
header('Content-type: application/json; charset=utf-8', true,200);
echo json_encode(array('r'=>$ping ));
} else {
print_r($_REQUEST);
}
} else {
print_r($_REQUEST);
}
Code: Alles auswählen
GNU nano 2.2.6 File: req.py Modified
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import requests
url = 'http://*****.**/includes/webservice/ws-dvi.php'
payload = {'s':{'1':'140536','t1':'1,2','t2':'1,2','t3':'1,2','t4':'1,2','t5':'1,2','t6':'1,2','t7':'1,2','t8':'1,2','t9':'1,2','t10':'1,2','t11':'1,2','t12':'1,2','b1':'1,2','b2':'1,2'}}
header = {'content-type': 'application/json'}
r = requests.post(url, json=payload, headers=header)
print(r.status_code)
print(r.headers)
print(r.text)
Wenn ich auch GET wechsel funktioniert das aber das ist keine option.
Ich hoffe ihr könnt mir helfen..
Euer T1bbY