使用 telnet 运行 python 脚本

编程入门 行业动态 更新时间:2024-10-22 10:46:40
本文介绍了使用 telnet 运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-05-01 06:47:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   telnet   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!