• 1 Votes
    8 Posts
    1k Views
    SchamperS
    I did something similar back with Boblight a year or 2 ago. Had it working with configurable lighting effects and Facebook/Email too. https://www.youtube.com/watch?v=RM946iXWEx8 The lighting transition was generated as well since Boblight only took byte streams and didn’t know about any effects. Kind of related to this, I currently have my Hyperion set up with 2 different sources, Kodi and an USB video grabber but I needed a way to easily switch them. My first solution was to create a Kodi plugin that I could control using a simple HTTP request. It worked but was tedious. I recently learned about the existence of cec-client and it’s monitoring mode, so I whipped up a bash script that monitors the CEC traffic and changes Hyperion sources based on my AV receiver’s source. All configurable too. The only other thing I want now is to be able to change the HDMI OUT mode of my receiver but apparently that’s impossible using CEC. There’s an undocumented command you can send over telnet to do it though but I don’t want to get another ethernet cable there.
  • Setting up Flask to work with Nginx

    General Computing flask nginx python uwsgi
    1
    1 Votes
    1 Posts
    459 Views
    ScuzzS
    After struggling for a few hour last night to get a working hello world with Flask and Nginx I think I should get a little guide up to make it a bit more clear. There are a few tutorials on various blog sites but non of them seemed to work correctly. Some had conflicting information and other had bits of code or config files that did not seem relevant. Even the Flask documentation was hard to understand and didn’t seem to work. I assume that you already have Nginx, Python and pip installed. Flask Flask is a lightweight web framework for Python. It is based on Werkzeug and Jinja 2. It is easy to install and run. You must have python and pip installed first before you can install Flask. To install you just run pip install Flask from your command line. For a quick demo, create a hello.py and copy in the following code: from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() Then type python hello.py and visit http://localhost:5000 to see your first Flask hello world app. More information can be found on the Flask Website I have installed Flask in a virtual environment. To do this you will need to run pip install virtualenv. Create your directory for you app and run virtualenv venv, venv can be any name you want but I kept it simple. To activate the virtual environment run source venv/bin/activate. This will activate your virtual environment. From here you will install Flask with the same command as above, pip install Flask. If you have created the hello.py file from above you should be able to run this from within the virtual environment and check http://localhost:5000 to see it running. Run deactivate from within the virtual environment to deactivate it. uWSGI Nginx is a web server that serves static files. It is unable to serve a python application. This is where uWSGI comes in. You will require build-essential and python-dev for uWSGI. Run pip install uwsgi to install uWSGI. Open up your config file in the sites-available directory inside your Nginx installation and copy in the following: server { listen 80; server_name localhost; charset utf-8; client_max_body_size 75M; location / { try_files $uri @yourapplication; } location @yourapplication { include uwsgi_params; uwsgi_pass unix:/var/www/demoapp/demoapp_uwsgi.sock; } } Changing the path to your apps path. You will no need to create the .ini file for you application. Create a yourapp.ini file in the directory your application is located in. Copy in the following: [uwsgi] #application's base folder base = /var/www/demoapp #python module to import app = hello module = %(app) home = %(base)/venv pythonpath = %(base) #socket file's location socket = /var/www/demoapp/%n.sock #permissions for the socket file chmod-socket = 666 #the variable that holds a flask application inside the module imported at line #6 callable = app #location of log files logto = /var/log/uwsgi/%n.log Make sure to change the paths to be the correct paths for your app location. Create the log file directory. Now run uwsgi --ini /path/to/your/yourapp.ini. Now if you open your domain name that is linked to your app you should see that the app has loaded and “Hello World!” is displayed. This is a quick and dirty memory dump on what I did last night to get it running on my server. Hopefully it is easy enough to follow and is useful to some people.
  • List of Python resources

    Development and Coding python learning resources
    5
    1 Votes
    5 Posts
    1k Views
    S
    @Scuzz They have an ebook version that looks good but I’ve not been able to find a download for it anywhere.
  • Some Encryption program in Python

    Development and Coding python enryption
    1
    1 Votes
    1 Posts
    846 Views
    ScuzzS
    Encryption program for a university module Usage: python joshcrypt.py [-h] [-e] [-d] keyword inputFile outputFile Flags: [-h] Help. This flag will generate help from the command line. [-e] Encrypt. Use this flag to encrypt the inputFile. [-d] Decrypt. Use this flag to decrypt the inputFile. Arguments: Keyword - The keyword used to encrypt the file. inputFile - The Filename to save the encrypted/decrypted file as. This program needs to be run from the command line; cmd.exe or any terminal emulator will suffice. To start the encryption process a user must type: python joshcrypt.py -e "keyword" "inputfile" "outputfile" Keyword is the password used for the document. Inputfile is the file that you want to be encrypted. Outputfile is the file that you want the encrypted file to be saved as. To start the decryption process a user must type: python joshcrypt.py -d "keyword" "inputfile" "outputfile" Keyword is the password used for the document. Inputfile is the file that you want to be decrypted. Outputfile is the file that you want the plain text file to be saved as. # Requires Python 2.7 as Python 3.0+ contains bugs with the array.array() function. import argparse import array import sys # Opens file as a byte array. def getBlock(fileName): f = open(fileName, "rb") blockArray = array.array('b', f.read()) f.close return blockArray # Converts bytes to string and writes it to a file. def writeBlocks(blocks, fileName): f = open(fileName, "wb") string = blocks.tostring() f.write(blocks) f.close() # Generates a key. # Converts the key into a byte array, creates another byte array of the key but reversed. # Xor the bytes together to generate the final key. def genKey(key): a = array.array('b', key) b = a[::-1] for x in range(len(a)): a[x] = a[x] ^ b[x] return a # Xor the bytes of the key with the bytes of the file. # Loops through the key and adds the times it has looped to the end of the key def encrypt(blocks, key): i = 0 r = 0 for block in range(len(blocks)): blocks[block] = blocks[block] ^ key[i] if i == len(key) - 1: i = 0 r += 1 key = genKey(key.tostring() + str(r)) else: i += 1 return blocks def decrypt(blocks, key): i = 0 r = 0 for block in range(len(blocks)): blocks[block] = blocks[block] ^ key[i] if i == len(key) - 1: i = 0 r += 1 key = genKey(key.tostring() + str(r)) else: i += 1 return blocks # Sets up the command line arguments # Calls functions to encrypt the file. def main(): parser = argparse.ArgumentParser(description="Encrypt and Decrypt a file.") parser.add_argument("-e", action = "store_true", default = False, dest = "edFlag", help = "Encrypt File") parser.add_argument("-d", action = "store_false", default = False, dest = "edFlag", help = "Decrypt File") parser.add_argument("keyword", action = "store", help = "Keyword used to encrypt or decrypt file") parser.add_argument("inputFile", action = "store", help = "Filename to be encrypted/decrypted") parser.add_argument("outputFile", action = "store", help = "Filename to save the encrypted/decrypted file as") args = parser.parse_args() if len(args.keyword) < 10 or len(args.keyword) > 40: print "Password needs to be greater than 10 characters and less than 40" sys.exit(1) if args.edFlag: print "ENCRYPTING" key = genKey(args.keyword) blocks = getBlock(args.inputFile) encrypted = encrypt(blocks, key) writeBlocks(encrypted, args.outputFile) else: print "DECRYPTING" key = genKey(args.keyword) blocks = getBlock(args.inputFile) decrypted = decrypt(blocks, key) writeBlocks(decrypted, args.outputFile) if __name__ == '__main__': main()```
  • Sending Emails with Python and Mailgun

    General Computing python mailgun emails
    2
    2 Votes
    2 Posts
    1k Views
    S
    Requests really does make HTTP usage so easy in Python; I came across and used it last week.