LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Best/Easiest way to implement a Python script as a webpage? (https://www.linuxquestions.org/questions/programming-9/best-easiest-way-to-implement-a-python-script-as-a-webpage-4175612589/)

snowman81 08-24-2017 09:34 PM

Best/Easiest way to implement a Python script as a webpage?
 
I have a Python script that takes an encoded URL from Proofpoint and decodes it. You call it with the encoded URL as an argument. I want to be able to have a very simple webpage on a server that just has an input box for the URL and underneath is the decoded output. I'm thinking of trying to implement it with Nginx+FastCGI maybe? Are there easier ways to do this as a non-coder? Thanks.

Code:

#!python

import sys
import re
import urllib.parse
import html.parser

def main():
        rewrittenurl = sys.argv[1]
        match = re.search(r'https://urldefense.proofpoint.com/(v[0-9])/', rewrittenurl)
        if match:
                if match.group(1) == 'v1':
                        decodev1(rewrittenurl)
                elif match.group(1) == 'v2':
                        decodev2(rewrittenurl)
                else:
                        print('Unrecognized version in: ', rewrittenurl)
                       
        else:
                print('No valid URL found in input: ', rewrittenurl)
               
def decodev1 (rewrittenurl):
        match = re.search(r'u=(.+?)&k=',rewrittenurl)
        if match:
                urlencodedurl = match.group(1)
                htmlencodedurl = urllib.parse.unquote(urlencodedurl)
                url = html.parser.HTMLParser().unescape(htmlencodedurl)
                print(url)
        else:
                print('Error parsing URL')

def decodev2 (rewrittenurl):
        match = re.search(r'u=(.+?)&[dc]=',rewrittenurl)
        if match:
                specialencodedurl = match.group(1)
                trans = str.maketrans('-_', '%/')
                urlencodedurl = specialencodedurl.translate(trans)
                htmlencodedurl = urllib.parse.unquote(urlencodedurl)
                url = html.parser.HTMLParser().unescape(htmlencodedurl)
                print(url)
        else:
                print('Error parsing URL')
               
if __name__ == '__main__':
    main()


Sefyir 08-24-2017 09:44 PM

Haven't done this myself, but these may be useful links:

https://fragments.turtlemeat.com/pythonwebserver.php
https://stackoverflow.com/questions/...te-with-python

pan64 08-25-2017 01:35 AM

additionally you can try flask: https://www.fullstackpython.com/flask.html
(and there are a lot of other possibilities)

dugan 08-25-2017 02:24 AM

Flask is a good option. This is another one, fairly similar to Flask:

http://bottlepy.org/docs/dev/

sundialsvcs 08-25-2017 07:45 AM

Django. Easily the best.

"The web framework for perfectionists with deadlines.™"


All times are GMT -5. The time now is 07:47 AM.