Python--get and Post

Source: Internet
Author: User
Tags urlencode

#python3 Get and post simple package

From urllibImport request, Parse
Import JSON


def requestmethod (Methodr, URL, data_dict, headers_dict):
if Methodr = =' GET ':
req = Request. Request
If data_dict:
Data_str = Parse.urlencode (data_dict)
Url_str = URL +'? ' + data_str
If headers_dict:
req = Request. Request (URL_STR,HEADERS=HEADERS_DICT)
Else
req = Request. Request (URL_STR)
Else
If headers_dict:
req = Request. Request (URL,HEADERS=HEADERS_DICT)
Else
req = Request. Request (URL)
With Request.urlopen (req)As response:
result = Json.loads (Response.read ())
return result
elif Methodr = = ' POST ':
req = Request. Request
Data_str = Parse.urlencode (data_dict). Encode ( ' utf-8 ')
if headers_dict:
req = Request. Request (URL, data=data_str, headers=headers_dict)
Span style= "COLOR: #000080" >else:
req = Request. Request (URL, data=data_str)
with Request.urlopen ( REQ) as response:
result = Json.loads (Response.read ())
return result
else:
Raise ' error ' < Span style= "COLOR: #000080" > < Span style= "COLOR: #000080" > < Span style= "COLOR: #660099" > < Span style= "COLOR: #000080" >

Python--get and Post

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.