2017-02-26 71 views
0

我正在运行一个带有自定义httpSimpleServer的小型Python应用程序以适应'POST'方法。错误:root:MiniFieldStorage - 运行在httpSimpleServer上的Python和Flask的问题

应用程序应该大写输入到表单中的所有字母并返回它们。

我得到一个错误,在终端,我GOOGLE了无济于事,我需要改变以获得此应用程序的工作?

的index.html

<!DOCTYPE html> 
<html lang="en"> 
<body> 
    <h1>Enter some text</h1> 
    <h2>(it will be converted to uppercase)</h2> 
    <form action="." method="POST" name="text"> 
     <input type="text"> 
     <input type="submit" name="my-form" value="Send"> 
    </form> 
</body> 
</html> 

flask_app.py

from flask import Flask 
from flask import request 
from flask import render_template 

app = Flask(__name__) 

@app.route('/') 
def my_form(): 
    return render_template("templates/index.html") 

@app.route('/', methods=['POST']) 
def my_form_post(): 
    text = request.form.index.html 
    processed_text = text.upper() 
    return processed_text 

if __name__ == '__main__': 
    app.debug = True 
    app.run() 

SimpleServer.py

import SimpleHTTPServer 
import SocketServer 
import logging 
import cgi 
import cgitb 

cgitb.enable() 

PORT = 8000 

class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): 

def do_GET(self): 
    logging.error(self.headers) 
    SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) 

def do_POST(self): 
    logging.error(self.headers) 
    form = cgi.FieldStorage(
     fp=self.rfile, 
     headers=self.headers, 
     environ={'REQUEST_METHOD':'POST', 
       'CONTENT_TYPE':self.headers['Content-Type'], 
       }) 
    for item in form.list:logging.error(item) 
    SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) 


Handler = ServerHandler 

httpd = SocketServer.TCPServer(("", PORT), Handler) 

print "serving at port", PORT 
httpd.serve_forever() 

终端错误消息

ERROR:root:Host: localhost:8000 
Connection: keep-alive 
Content-Length: 12 
Cache-Control: max-age=0 
Origin: http://localhost:8000 
Upgrade-Insecure-Requests: 1 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3)   
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36 
Content-Type: application/x-www-form-urlencoded 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Referer: http://localhost:8000/templates/ 
Accept-Encoding: gzip, deflate, br 
Accept-Language: en-US,en;q=0.8 
Cookie: Pycharm-2c79b07b=b4744dcc-07ae-4e39-9d94-06d7a2fcf11c 

ERROR:root:MiniFieldStorage('my-form', 'Send') 
127.0.0.1 - - [26/Feb/2017 22:11:21] "POST /templates/ HTTP/1.1" 200 - 

回答

0

关于SimpleServer.py

ERROR:root:MiniFieldStorage('my-form', 'Send') 

是不是一个错误 - 是的只需登录提交表单的内容。由于您致电logging.error(),因此消息前缀为“ERROR:root”。

可以通过先加入这一行不久的文件

logging.basicConfig(level=logging.DEBUG) 

设置日志级别的顶部,并呼吁改变从logging.error()logging.debug()使用logging.debug()代替。

index.html中的HTML表单也存在问题;它有一个未命名的字段<input type="text">。如果您希望浏览器提交该字段,则必须命名它。

<input type="text" name="phrase"> 

如果您现在登录表单,您会看到它出现在日志中。现在


,转向瓶的应用程序,这条线:

text = request.form.index.html 

将会给你带来问题,因为你是如何进入字段的形式发布的,这不是。试试这个:

text = request.form.get('phrase', '') 

这将让名为"phrase"从表单中字段的值(这是一个字典查找)。如果该字段未设置,该值将默认为空字符串 - 这提供了一些容错。

0

请检查此代码。

索引。HTML

<!doctype><html><meta charset="utf-8"><form id="form" method="post"> 
<label>First Name </label> 
<input type="text" name="firstname" id="firstname" /><br><br> 
<input type="submit" name="submit" id= "submit"/></form><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script><script>var API_URL = 'http://' + location.host;$("#submit").click(function(){ 
var f = $("#firstname").val(); 
var form_data = new FormData(); 
form_data.append('firstname', f); 
$.ajax({ 
    url: API_URL, 
    type : "POST", 
    contentType: false, 
    processData: false, 
    dataType : 'json', 
    data : form_data, 
    success : function(result) { 
     console.log(result);}, 
    error: function(xhr, resp, text) { 
     console.log(xhr, resp, text); 
    }})});</script></html> 

server.py

import os 
from flask import Flask, render_template, jsonify, request 
import json 


__author__ = 'Rajshekhar Horatti' 
app = Flask(__name__) 
APP_ROOT = os.path.dirname(os.path.abspath(__file__)) 

@app.route("/") 
def main(): 
    return render_template("index.html") 


@app.route("/", methods=['POST']) 
def Createdata(): 
    target = os.path.join(APP_ROOT) 
    firstname = request.form['firstname'] 
    data = firstname.upper() 
    return data 


if __name__ == "__main__": 
    app.run(host='localhost', port=8080, debug=True)