Using Python3 's requests module to simulate get and post requests is very simple and powerful, you can construct header headers, pass various types of parameters, cookies,session, and so on to simulate different requests, and here are just the simplest examples of get and post.
First, write a method in your PHP project that you can request via http:
Public Function pyrequests ()
{
$x = I (' x ');
$y = I (' y ');
$data = [
' AA ' => ' aaaaaaaa ',
' BB ' => ' bbbbbbbb ',
' Hehe ',
32,
' x ' => $x,
' Y ' => $y,
];
echo Json_encode ($data); exit;
}
This is written in the TP framework, and I represents the parameter that gets the pass of a GET or POST request. Visit the URL for example: http://www.xx.com/index/pyrequests
Write a py file:
__author__ = ' Tanteng '
Import requests
Def testrequests ():
url = ' http://www.xxx.com/index/pyrequests '
params = {
' x ': ' xxxx ',
' Y ': ' yyyy '
}
Re = Requests.get (url,params)
return re
if __name__ = = ' __main__ ':
Re = testrequests ()
Print (Re.text)
Run this py file and get the result:
{"AA": "aaaaaaaa", "BB": "bbbbbbbb", "0": "hehe", "1":, "x": "xxxx", "y": "yyyy"}
That means that the GET request was successfully sent through the Python3 requests module and the returned data was obtained. Here the first parameter of the Requests.get method represents the URL of the request, the second parameter represents the passed parameter, and the header, the cookie, and so on are set. The Requests.post method uses a similar, send POST request