【发布时间】:2021-12-14 14:48:51
【问题描述】:
我想把我的curl to urllib.request命令转换成python,curl命令:
curl -v -i -X POST http://api.textart.io/img2txt.json --form image=@path/to/dir/file.jpg
我的代码:
import json
from urllib import request, parse
data = parse.urlencode({
"image": open("path/to/dir/file.jpg", "rb")
}).encode()
req = request.Request("http://api.textart.io/img2txt.json")
req.add_header('User-Agent', 'Mozilla/5.0')
response = json.loads(request.urlopen(req, data).read())
response = json.dumps(response, indent=4)
print(response)
回复:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at
webmaster@api.textart.io to inform them of the time this error occurred,
and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>
虽然 curl 有效,但请帮忙。
【问题讨论】:
-
谢谢,但现在我有一个新错误,我编辑我的答案
-
如果你会使用
requests,那么你可以使用门户curlconverter.com来转换它。 -
您可以在
curl和python中使用地址httpbin.org/post - 它应该发回您发送到此网址的所有信息 - 您可以比较结果以查看差异。 -
我想我看到了一个问题 - 在 Python 中,您发送
GET而不是POST
标签: python curl request urllib pycurl