`requests.post`无法正确连接

时间:2019-03-08 21:29:54

标签: python curl

我有一个要在R中连接的模型。我通过运行打开模型;

> library(plumber)
> r <- plumb("deploy_ml_credit_model.R")
Warning message:
In readLines(file) :
  incomplete final line found on 'deploy_ml_credit_model.R'
> r$run(swagger = FALSE)
Starting server to listen on port 3582

然后我转到Python并运行以下命令;

Python代码:

import requests
import json
response = requests.post(
    “http://127.0.0.1:3582”
    , headers={“Content-Type”: “application/json”}
    , data=json.dumps({
        "Status.of.existing.checking.account": "A11"
    , "Duration.in.month": 24
    , "Credit.history": "A32"
    , "Savings.account.bonds": "A63"
    })
)

print response.json()

在哪里出现以下错误:

  File "<ipython-input-41-507692393845>", line 4
    “http://127.0.0.1:3582”
        ^
SyntaxError: invalid character in identifier

我在浏览器中打开链接,并收到以下消息:{"error":["404 - Resource Not Found"]}

我在哪里出错呢?我有plumber在RStudio的后台运行,因此应该可以连接。关于权限或防火墙问题,我有什么问题吗?

编辑:

当我输入http://127.0.0.1:3582/predict时,仍收到以下消息-错误http://127.0.0.1:3582/predict。我清理代码,并收到以下错误:

代码2:

import requests
import json
response = requests.post(
    "http://127.0.0.1:3582"
    , headers={"Content-Type": "application/json"}
    , data=json.dumps({
        "Status.of.existing.checking.account": "A11"
    , "Duration.in.month": 24
    , "Credit.history": "A32"
    , "Savings.account.bonds": "A63"
    })
)


print response.json()

错误2:

  File "<ipython-input-49-0669c2ac9d9d>", line 15
    print response.json()
                 ^
SyntaxError: invalid syntax

编辑3:

我键入:

print (response)

我收到此错误:

<Response [404]>

但是R模型在后台运行

编辑:

使用:

curl -X POST "http://127.0.0.1:8000/__swagger__/predict" -d '{"Status.of.existing.checking.account": "A11", "Duration.in.month": 24, "Credit.history": "A32", "Savings.account.bonds": "A63"}' -H  "accept: application/json"

给出此错误:

curl -X POST "http://127.0.0.1:8000/__swagger__/predic
t" -d '{"Status.of.existing.checking.account": "A11", "Duration.in.month": 24, "Credit.history":
 "A32", "Savings.account.bonds": "A63"}' -H  "accept: application/json"
<h1>Bad Request</h1>curl: (6) Could not resolve host: A11,
curl: (6) Could not resolve host: Duration.in.month
curl: (6) Could not resolve host: 24,
curl: (6) Could not resolve host: Credit.history
curl: (6) Could not resolve host: A32,
curl: (6) Could not resolve host: Savings.account.bonds
curl: (3) [globbing] unmatched close brace/bracket in column 4

编辑:

我运行以下行:

curl POST -H 'Content-Type:accept:application/json' -d "{/"Credit.history/":/"A32/"}" http://127.0.0.1:8000/__swagger__/predict

所以我决定删除很多我想发送给模型的内容,只剩下一个变量,现在我遇到了这个错误:

curl POST -H 'Content-Type:accept:application/json' -d "{/"Credit.history/":/"A32/"}" http://127.0.0.1:8000/__swagger__/predict
curl: (6) Could not resolve host: POST
{"error":["500 - Internal server error"],"message":["Error: lexical error: invalid char in json text.\n                                     {/Credit.history/:/A32/}\n
           (right here) ------^\n\n"]}

2 个答案:

答案 0 :(得分:2)

字符串中的引号很奇怪

“http://127.0.0.1:3582”

–它们应该是直接引号:

"http://127.0.0.1:3582"

答案 1 :(得分:1)

您的“错误2”似乎是指将Python 3与Python 2语法一起使用。 print是Py3中的常规函数​​,因此您需要在其周围使用括号:

import requests
import json

response = requests.post(
    "http://127.0.0.1:3582",
    headers={"Content-Type": "application/json"},
    data=json.dumps(
        {
            "Status.of.existing.checking.account": "A11",
            "Duration.in.month": 24,
            "Credit.history": "A32",
            "Savings.account.bonds": "A63",
        }
    ),
)


print(response.json())