Skip to content Skip to sidebar Skip to footer

Python With Flask, Connect To Api

As it was advice me to do on an early post, to do an web server with an public api. Basically I want to do an app that will run on windows and will get the data from the API. from

Solution 1:

This a good start. What I would definitely change is to return JSON data. For your current use case it seems that you want to only return a list of filenames, so this should work.

import sqlite3 as sql

from flask import Flask, jsonify

app = Flask(__name__)


@app.route('/')defget_data():
    con = sql.connect("AntiCheat.db")
    con.row_factory = sql.Row

    cur = con.cursor()
    cur.execute("SELECT filename FROM files")
    rows = cur.fetchall()

    response = [row['filename'] for row in rows]
    return jsonify(response)


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

Then your client can fetch the file names from the API with:

requests.get('http://127.0.0.1:5000/').json()

Now, if you require anything more from the API I would strongly suggest to look into the Flask-RESTful package.


Another point: if the idea of your application is a simple anti-cheat that works by looking for some files from the DB on the client's computer, checking for file names won't be very successful: the client can change name of files or modify legit game files, inserting malicious code, but keeping the original name.

Instead of checking for file names, I would recommend you to check for files MD5 checksums. You can check that some of the core game files have the expected checksum (i.e. they haven't been tampered with), and also look for some blacklisted checksums of known bad files.

Getting the checksum of a file from python is really easy:

deffile_checksum(file_path):
    hash_md5 = hashlib.md5()
    withopen(file_path, 'rb') as file:
        for chunk initer(lambda: file.read(4096), b''):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

If you have a UNIX system you most likely will also be able to do md5 <filename> from your terminal, and you would get the same checksum than from the python function.

Post a Comment for "Python With Flask, Connect To Api"