简体   繁体   English

用telnet运行python脚本

[英]Run python script with telnet

I'm using localhost to testing python scripts and I need to test it via telnet, where I'm using PuTTY. 我正在使用localhost来测试python脚本,我需要通过telnet测试它,我在使用PuTTY。 I have this python script: 我有这个python脚本:

@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"))
    db.commit()
    print("Saved")
    return "Saved"

And now I don't know how to connect to 127.0.0.1/add and how put values into POST method. 现在我不知道如何连接到127.0.0.1/add以及如何将值放入POST方法。 I can't use connection like this: 我不能使用这样的连接:

$ o 127.0.0.1/add

And if I try this: 如果我试试这个:

$ o 127.0.0.1
$ GET / HTTP/1.0
$ Host: 127.0.0.1/add

then the result is 404 NOT FOUND (via browser it works, but only with static insert values and GET method). 然后结果是404 NOT FOUND(通过浏览器它可以工作,但只有静态插入值和GET方法)。 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? 所以我想问一下如何连接到这个地址127.0.0.1/add并获取POST方法的值,以便将它们用作数据库的插入值? Thank you 谢谢

use curl instead : curl -v -X POST 'http://<ip.address.of.server>/add' --data "title=foo&text=baz and spam and eggs" 改为使用curl: curl -v -X POST 'http://<ip.address.of.server>/add' --data "title=foo&text=baz and spam and eggs"

for a post in telnet you need to specify the body of the request like this : 对于telnet中的帖子,您需要指定请求的主体,如下所示:

POST /add HTTP/1.1
Content-type:application/x-http-form-urlencoded

title=foo&text=baz+and+spam+and+eggs

If you want to build a query string from a dictionary you need urllib 如果要从字典构建查询字符串,则需要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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM