I have been learning to use Python recently and found that there is very little mention of how to use post on the Internet, so the following article mainly introduces to you the methods of submitting data using post and get methods in Python. The article introduces it in detail through example code. , it has certain reference and learning value for everyone’s study or work. Friends who need it can come and take a look below.
Preface
In the process of using Python recently, I found that there are few mentions on the Internet about how to pass an array as Examples of parameters. Here are relevant examples based on my own practical experience. I won’t go into more details below. Let’s learn with the editor.
Examples are as follows:
Simple post request:
def http_post(): url = "http://152.1.12.11:8080/web" postdata = dict(d=2, p=10) post = [] post.append(postdata) req = urllib2.Request(url, json.dumps(post)) #需要是json格式的参数 req.add_header('Content-Type', 'application/json') #要非常注意这行代码的写法 response = urllib2.urlopen(req) result = json.loads(response.read()) print result
When a token is required, the writing method is as follows:
def http_post(): url = "http://152.1.12.11:8080/web" postdata = dict(d=2, p=10) post = [] post.append(postdata) req = urllib2.Request(url, json.dumps(post)) access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1bmlxdWVfbmFtZSI6I..........' req.add_header('Authorization', access_token) #header中添加token req.add_header('Content-Type', 'application/json') #要非常注意这行代码的写法 response = urllib2.urlopen(req) result = json.loads(response.read()) print result
The get method is written as follows:
def get_access_token(): local_url = 'http://152.1.1.1:8080/web' response = urllib2.urlopen(local_url).read() resp = json.loads(response) print resp
Summary
The above is the detailed content of Sharing of code examples for submitting data using post and get methods in Python. For more information, please follow other related articles on the PHP Chinese website!