英文:
How do I send a post request to a website's server in python?
问题
我想在Python中向google.com的服务器发送一个POST请求,而不是GET请求。
到目前为止,我的代码如下:
import socket
HOST = 'www.chase.com'
PORT = 80
socket = socket.socket()
socket.connect((HOST, PORT))
socket.send(b"""
GET / HTTP/1.1
Host: chase.com
""")
msg = socket.recv(1024)
print(msg)
有人能帮我发送一个POST请求到google.com吗?
英文:
I want to send a post request instead of a GET request to google.com's server in python.
My code so far is:
import socket
from http.server import HTTPServer, BaseHTTPRequestHandler
HOST = 'www.chase.com'
PORT = 80
socket = socket.socket()
socket.connect((HOST, PORT))
socket.send(b"""
GET / HTTP/1.1
Host: chase.com
""")
msg = socket.recv(1024)
print(msg)
Can someone help me send a post request to google.com?
答案1
得分: 1
你可以使用 requests 模块:
import requests
data = {"你想要发布的内容": "以字典形式"}
url = "你的URL"
response = requests.post(url, json=data)
print(response)
在此之前,你需要运行以下命令来安装 requests 模块:
pip install requests
英文:
You can use the requests module :
import requests
data = {"what you want to post":"in form of dict"}
url = "your url"
response = requests.post(url, json=data)
print(response)
before, you have to run this command to install requests module :
pip install requests
答案2
得分: 0
以下是您要翻译的内容:
Can someone help me send a post request to google.com?
这听起来像是使用 urllib.request
(它是标准库的一部分)的任务,考虑以下简单的示例:
import urllib.request
import urllib.parse
data = urllib.parse.urlencode({'x': 1, 'y': 2, 'z': 3})
data = data.encode('ascii')
with urllib.request.urlopen("http://httpbin.org/post", data) as f:
print(f.read().decode('utf-8'))
输出结果为:
{
"args": {},
"data": "",
"files": {},
"form": {
"x": "1",
"y": "2",
"z": "3"
},
"headers": {
"Accept-Encoding": "identity",
"Content-Length": "11",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Python-urllib/3.8",
"X-Amzn-Trace-Id": "Root=1-63efb182-4f75d5cd47cbe6916985ef10"
},
"json": null,
"origin": "213.134.180.174",
"url": "http://httpbin.org/post"
}
我在示例中使用了 httpbin.org
,因为它对于调试发送请求很有用,它会响应接收到的内容。请注意,您提供的 data
应该是 bytes
,因此您需要对其进行 .encode
处理,而您获得的响应是 bytes
,如果您想要文本(str
),则需要进行 .decode
处理。
英文:
> Can someone help me send a post request to google.com?
This sounds like task for urllib.request
(which is part of standard library), consider following simple example
import urllib.request
import urllib.parse
data = urllib.parse.urlencode({'x':1,'y':2,'z':3})
data = data.encode('ascii')
with urllib.request.urlopen("http://httpbin.org/post", data) as f:
print(f.read().decode('utf-8'))
gives output
{
"args": {},
"data": "",
"files": {},
"form": {
"x": "1",
"y": "2",
"z": "3"
},
"headers": {
"Accept-Encoding": "identity",
"Content-Length": "11",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Python-urllib/3.8",
"X-Amzn-Trace-Id": "Root=1-63efb182-4f75d5cd47cbe6916985ef10"
},
"json": null,
"origin": "213.134.180.174",
"url": "http://httpbin.org/post"
}
I have used httpbin.org
in my example as it is useful for debugging sending request, as it does respond with what it get. Observe that what you provide as data
should be bytes
so you need to .encode
it and what you get as response is bytes
so you need to .decode
it if you want text (str
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论