问题描述
限时送ChatGPT账号..我使用 localhost 来测试 python 脚本,我需要通过 telnet 来测试它,我使用的是 PuTTY.我有这个 python 脚本:
I'm using localhost to testing python scripts and I need to test it via telnet, where I'm using PuTTY. I have this python script:
@app.route('/add/', methods=['POST'])
def add_entry():
db = get_db()
db.execute('insert into entries (title, text) values (?, ?)',
("value_from_telnet", "another_value_from_telnet"))
dbmit()
print("Saved")
return "Saved"
现在我不知道如何连接到 127.0.0.1/add 以及如何将值放入 POST 方法中.我不能使用这样的连接:
And now I don't know how to connect to 127.0.0.1/add and how put values into POST method. I can't use connection like this:
$ o 127.0.0.1/add
如果我试试这个:
$ o 127.0.0.1
$ GET / HTTP/1.0
$ Host: 127.0.0.1/add
然后结果是 404 NOT FOUND(通过浏览器它可以工作,但只能使用静态插入值和 GET 方法).所以我想问一下我如何连接到这个地址 127.0.0.1/add 并将值获取到 POST 方法中以将它们用作数据库的插入值?谢谢
then the result is 404 NOT FOUND (via browser it works, but only with static insert values and GET method). So I would like to ask how can I connect to this address 127.0.0.1/add and get values into POST method to use them as insert values to the database? Thank you
推荐答案
改用 curl : curl -v -X POST 'http://<ip.address.of.server>/add' --数据title=foo&text=baz 和垃圾邮件和鸡蛋"
use curl instead : curl -v -X POST 'http://<ip.address.of.server>/add' --data "title=foo&text=baz and spam and eggs"
对于 telnet 中的帖子,您需要像这样指定请求的正文:
for a post in telnet you need to specify the body of the request like this :
POST /add HTTP/1.1
Content-type:application/x-http-form-urlencoded
title=foo&text=baz+and+spam+and+eggs
如果你想从字典中构建查询字符串,你需要 urllib
If you want to build a query string from a dictionary you need urllib
import urllib
params = {title:"foo",text:"baz and spam and eggs"}
print """
POST /add HTTP/1.1
Content-type:application/x-http-form-urlencoded
""",urlencode(params)
这篇关于使用 telnet 运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论