Gustavo Fringe
Gustavo Fringe

Reputation: 21

How to call a internet Python script from another local Python script

I have a Python script that does some complicated statistical calculations on data and want to sell it. I thought about compiling it, but I have read that Python compiled with py2exe can be easily decompiled. So I tried another aproach, namely running the script from the Internet.

Now my code, in file local.py looks like:

local.py

#imports
#define variables and arrays

#very complicated code i want to protect

I want to obtain something like:

File local.py:

#imports
#define variables and arrays

#call to online.py
print(result)

File online.py:

#code protected
#exit(result)

Is there is a better way to do this ? I want client to run the code without being able to see it.

Upvotes: 2

Views: 150

Answers (1)

CryptoFool
CryptoFool

Reputation: 23089

A web API would work great for this, which is a super simple thing to do. There are numerous Python frameworks for creating simple HTTP APIs, and even writing one from scratch with just low level sockets wouldn't be too hard. If you only want to protect the code itself, you don't even need security features.

If you only want people who've paid for the usage to have access to your API, then you'll need some sort of authentication, like an API key. That's a pretty easy thing to do too, and may come nearly for free from some of the aforementioned frameworks.

So your client's code might look something like this:

File local.py:

import requests

inputdata = get_data_to_operate_on()

r = requests.post('https://api.gustavo.com/somemagicfunction?apikey=23423h234h2g432j34', data=inputdata)
if r.status_code == 200:
    result = r.json()
    # operate on the resulting JSON here
    ...

This code does a HTTP POST request, passing whatever data is returned by the get_data_to_operate_on() call in the body of the request. The body of the response is the result of the processing, which in this code is assumed to be JSON data, but could be in some other format as well.

There are all sorts of options you could add, by changing the path of the request (the '/somemagicfunction' part) or by adding additional query parameters.

This might help you to get started on the server side: https://nordicapis.com/8-open-source-frameworks-for-building-apis-in-python. And here's one way to host your Python server code: https://devcenter.heroku.com/articles/getting-started-with-python

Upvotes: 1

Related Questions