SWDesk
JSON Data Transmission
bizmaker
2021. 1. 23. 15:44
1. Python to MySQL via PHP
2. MySQL to Python via PHP
1-1. Python source
def Test41():
ServerURL = "https://www.bilientsvc.net/ZezoUP/Test/ArticleCollector07.php"
REQUESTHEADER = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246"}
data11 = {
'k11': 'v11',
'k12': 'v12'
}
data1 = {
'RequestType': 'Test11',
'UserAddress': 'User11',
'Keyword': json.dumps(data11)
}
res1 = requests.post(ServerURL, headers=REQUESTHEADER, data=data1)
print(res1)
print(res1.text)
#collectionList = res1.json()
1-2. PHP source
function Test_11($conn){
$userAddress = $_POST['UserAddress'];
$keyWord = addslashes($_POST['Keyword']);
//echo $keyWord;
$Query1 = "insert into CollectionInfos(Useraddress, Keyword) values('".$userAddress."', '".$keyWord."');";
echo $Query1;
mysqli_query($conn, $Query1);
}
2-1. PHP source
function Test_12($conn){
$userAddress = $_POST['UserAddress'];
$keyWord = stripslashes($_POST['Keyword']);
$Query1 = "select * from CollectionInfos WHERE UserAddress='User11'";
$QueryResults = mysqli_query($conn, $Query1);
$QueryResultNumber = mysqli_num_rows($QueryResults);
if($QueryResultNumber < 1) return NULL;
$CIs1 = array();
$Index1 = 0;
while($Row1 = mysqli_fetch_array($QueryResults)){
$CI1 = new cCollectionInfo();
$CI1->SetValuebyRow($Row1);
$CIs1[$Index1] = $CI1->GetJSON();
$Index1++;
//echo $Index1."<br>";
}
$rst2['Results'] = $CIs1;
if($rst2['Results'] != NULL) echo json_encode($rst2);
else echo "NULL Results<br>";
}
2-2. Python source
def Test42():
ServerURL = "https://www.bilientsvc.net/ZezoUP/Test/ArticleCollector07.php"
REQUESTHEADER = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246"}
data1 = {
'RequestType': 'Test12',
'SNo':14
}
res1 = requests.post(ServerURL, headers=REQUESTHEADER, data=data1)
json1 = json.loads(res1.text)
print(json1)
for json11 in json1['Results']:
json1a = json11['Keyword']
json1b = json.loads(json1a)
print(json1a, '-', json1b['k12'])
#print(json11['Keyword']['k12'])
#collectionList = res1.json()
반응형