TringaliLuca

cowsay Flask webserver example

Jan 17th, 2020
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. apt-get install python3-pip net-tools nano cowsay
  4. pip3 install Flask==1.0.2
  5.  
  6. cat << EOF > /opt/cowsay.py
  7. import sys
  8. import os.path
  9. import subprocess
  10.  
  11.  
  12. from flask import Flask
  13. from flask import request
  14.  
  15. localport = 8000
  16. localhost = "0.0.0.0"
  17.  
  18. app = Flask(__name__)
  19.  
  20.  
  21. #http://0.0.0.0:8000/?text=My%20Text
  22. @app.route("/", methods=['GET'])
  23. def root():
  24.     myexec = "/usr/games/cowsay"
  25.     mytext = request.args.get('text', default = "Hello World", type = str)
  26.     try:
  27.         process = subprocess.Popen([myexec, mytext], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  28.         myimg =  process.communicate()[0]
  29.         myimg = myimg.decode("utf-8")
  30.     except:
  31.         myimg = ""
  32.     myhtml = "<pre>"+myimg.replace("\n", "</br>")+"</pre>"
  33.     return myhtml
  34.  
  35.  
  36.  
  37. if __name__ == "__main__":
  38.     app.run(debug=True, host=localhost, port=localport)
  39. EOF
  40.  
  41. chmod 755 /opt/cowsay.py
  42.  
  43. cat << EOF > /lib/systemd/system/cowsay.service
  44. [Unit]
  45. Description=Simple webserver that prints Cowsay
  46. [Service]
  47. ExecStart=python3 /opt/cowsay.py
  48. StandardOutput=null
  49. #Uncomment:
  50. #User=cowsay
  51. #Group=users
  52. #if you want to run script as a different user than root
  53. [Install]
  54. WantedBy=multi-user.target
  55. Alias=cowsay.service
  56. EOF
  57.  
  58.  
  59.  
  60.  
  61. systemctl enable cowsay.service
  62. systemctl start cowsay.service
RAW Paste Data