用Python给服务器发Post请求的简明例子

用Python给server发post request可以用强大的request库。以下是例子,几行就写完。包括import library,construct request,发送post请求,检查response,根据是否成功输出不同的信息。

import requests

url = 'http://your-server.com/api/endpoint'
data = {'key': 'value'}

response = requests.post(url, json=data)

if response.status_code == 200:
    print('Success!')
else:
    print('Failed with status code:', response.status_code)

Leave a Comment

Your email address will not be published.