`requests.post`未正确连接

问题描述 投票:0回答:2

我有一个我试图在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"]}
python curl
2个回答
2
投票

你在字符串中有非常奇怪的引号

“http://127.0.0.1:3582”

- 它们应该是直引号:

"http://127.0.0.1:3582"

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())
© www.soinside.com 2019 - 2024. All rights reserved.