PYTHON3 requests module get and post samples

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.