osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Cannot pass a variable given from url to route's callback fucntion and redirect issue


On Thursday, August 30, 2018 at 10:08:34 AM UTC-6, ????? ?????? wrote:
> I did try it with 'None' and as page='index.html' Flask return an error both ways (while bottle framework does not)

I think you are mistaken, making the change I suggested
fixes the "TypeError: index() missing 1 required positional 
argument" error you asked about.

Appended below is a runnable version of your code.  I couldn't
tell from your post whether you just left out code after the 
"if page !=..." statement for posting or whether is is acually
missing in your code.  I filled what was need to run.  Neither 
the html in your code or the temple are referenced by the code
you posted so you'll need put the pieces together.

> 2. Also about the redirect funtion iam using... is there a way to get back the HTML response of running that cgi-scrit and then add another HTML value to that response? I used subprocess and Response but they weren able to deliver the content back the pdata variable for display.
> What i actually want to do among other things is from within my flask app script to try to run and get theresponse back of a cgi-script

A subprocess should work if you run it with environment variables 
set up the way web server would call it with.  Alternatively
you could take the code in the cgi script (assuming its python)
and put it in a module function somewhere, then the cgi script
becomes a wrapper around a function call to it, and you can call 
the same function in your flask code.

One last point: redirecting to an arbitrary page based on a url 
provided by an untrusted user strikes me as dangerous.  I would 
at least whitelist the pages with something like:
   if page not in ('products', 'about', help'): abort (404)

Hope this helps...
------

from flask import Flask, redirect
app = Flask (__name__)

@app.route( '/' )
@app.route( '/<page>' )
def index( page='index.html' ):
        # use the variable form template for displaying
        pagehit = 333  # Tempory for testing
        counter = '''<center>
                        <table bgcolor=black bordercolor=orangered>
                        td><font size=3 color=lime> ??????? ??????????: </font></td>
        <td><a href="{{ url_for( '/log', page='%s' ) }}"><font size=3 color=plum> %d </font></a></td>
                                        </table>
                                ''' % (page, pagehit)
        if page != 'index.html':
            pdata = redirect( 'http://xxsuperhost.gr/cgi-bin/' + page )
        else: pdata = "Hi, I'm the index page"
        return pdata

@app.route('/log')
def log(): return "Hi, I'm the /log page"

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