【问题标题】:Send curl POST Request in Python without installing a lib在不安装库的情况下在 Python 中发送 curl POST 请求
【发布时间】:2021-07-07 09:47:20
【问题描述】:

我想在 python 中发送一个 curl post 请求。但我无法安装任何像“请求”这样的库。我可以像下面的例子一样发送 POST 请求:

curl -H "Content-Type: application/json" -X POST -d {\"username\":\"myusername\",\"password\":\"mypassword\"} https://example.com/login

我在 python2 中需要与上面相同的代码。然后,我必须阅读它返回的内容。我正在使用 Windows 10。

【问题讨论】:

  • 当我导入 urllib.request 时,我收到“ImportError: No module named request”错误。我无法安装任何库。
  • 如果您使用的是 Python 2,请尝试 urllib2.urlopen
  • 类似同样的错误 ImportError: No module named urlopen
  • 你应该导入urllib2并使用函数urllib2.urlopen

标签: python python-3.x python-2.7 curl post


【解决方案1】:

编写一个 curl 命令,使其可以在 shell 中工作,然后在 shell 中执行该命令。 这样就不需要 requests 包了。

import os
command = "curl -u {username}:{password} [URL]"
os.system(command)

【讨论】:

  • 你能帮我如何转换上面的例子吗?我做不到。
  • 检查您的代码是否在 linux 上运行:geeksforgeeks.org/curl-command-in-linux-with-examples
  • 不,我正在使用 Windows10。我添加了它作为注释。
  • 在这种情况下,您必须在 windows 终端 shell 中安装 cUrl。我想这不会是可行的。
  • 不,我们可以认为只是python方面的限制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
相关资源
最近更新 更多