Why does the Flask framework return the parameters of a GET request causing the Internal Server Error?

Source: Internet
Author: User

Reply content:

Start by clarifying these Python rules before making inferences.

1. Data is the incoming query parameters, according to the modern programming language of the play, is to be converted to a Unicode string, so that internationalization and localization is more convenient. So the data type is Unicode,type (data) Returns the result is Unicode. This Unicode is a python built-in type and is also a function.

2. After the type (data) ==unicode, printing directly in the Python console will get the " "This is because the Python console automatically calls the object's. __str__ () method when printing, to get a string that can be displayed, which is also handy for Python. You can manually execute STR (Unicode) in the Python console to get " " 。

3. How does the Web framework work? The type of return value required by the common Pythonweb framework in the request handler is STR, so it can be sent directly to the client. If you need a custom response header, you can use the response object built into the framework. However, the underlying basic of these frameworks is the direct implementation of the Wsgi interface, a python generic Web server interface. The Start_response () as above is actually flask when the function return value is detected as a function, he is treated as a wsgi function. The two parameters of the WSGI function are passed in, an environment of type dict, a function type Start_response functions. That is, the actual call is Unicode (environ,start_response).

4. The use of Unicode functions is to convert a string into a Unicode object. The first argument is a string, and the second parameter is the coded character set. Like what
>>> Print (Unicode (' Hello ', ' utf-8 '))
How are you doing

So, the problem with your program is this. Data is a Unicode type and is also a Unicode function. Flask as the return value, flask detects that this is a function, so as a WSGI application to handle, passed Environ and start_response two parameters. The two parameters that the Unicode function actually accepts are strings and character sets, so a parameter error is encountered.

If you need to know the type of data in flask, you can use STR (type data). This translates the output into a string that can be displayed, as you can see. In Flask, the Make_response is called first, based on the results returned by method handle.

def Make_response( Self, RV):    ...    if  not isinstance(RV,  Self.Response_class):        if isinstance(RV, (Text_type, bytes, ByteArray)):             RV =  Self.Response_class(RV, Headers=Headers, Status=Status)            Headers = Status = None        # RV is the result of method handle return, in K-God code 
       is        Else:             RV =  Self.Response_class.Force_type(RV, Request.environ)
Because type (data) returns a type, the function does not allow a return of type. Change to this:
return repr (data) may be a different mechanism, flask of the view return string and so on, even if you return the number is not good, recently I also in the web that piece, just toss the same, and you that public number seems to be a personal unauthenticated subscription number it, Developer mode does not have a custom menu, that is, the lack of a direct web portal, but also through the message to send links, very troublesome, limited a lot, and also to StackOverflow search will be better, basic can find the answer app.debug=true
You can see the detailed error as specified in this to return a string, then you have to return the string, the return of the other can not be used to explain any problem, also does not mean that the future version can still be used, when you return is not a string of time is undefined behavior, Just in web.py, this undefined behavior is just right for you. You can turn on debug mode. From Webpy source webpy/utils.py
def Safestr(obj, encoding=' Utf-8 '):    r "" "converts any given object to Utf-8 encoded string.    >>> safestr (' Hello ')' Hello '>>> safestr (U ' \u1234 ')' \xe1\x88\xb4 '>>> safestr (2)' 2 '    """    if isinstance(obj, Unicode):        return obj.encode(encoding)    elif isinstance(obj, Str):        return obj    elif hasattr(obj, ' Next '): # iterator        return Itertools.IMAP(Safestr, obj)    Else:        return Str(obj)
In the Flask application I wrote, methods was written in the App.route decorator.
Like this
@app. Route ('/', methods=[' GET ', ' POST ')
def func (args):


Why don't you just change it and try it?
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.