[英]How to execute a local python script into a docker from another python script?
[英]How to call a internet Python script from another local Python script
我有一个 Python 脚本,它对数据进行一些复杂的统计计算,并想出售它。 我想编译它,但我读过用py2exe
编译的Python可以很容易地反编译。 所以我尝试了另一种方法,即从 Internet 运行脚本。
现在我的代码,在文件local.py
看起来像:
本地文件
#imports
#define variables and arrays
#very complicated code i want to protect
我想获得类似的东西:
文件local.py
:
#imports
#define variables and arrays
#call to online.py
print(result)
文件online.py
:
#code protected
#exit(result)
有没有更好的方法来做到这一点? 我希望客户端在无法看到它的情况下运行代码。
Web API 对此非常有用,这是一件非常简单的事情。 有许多 Python 框架可用于创建简单的 HTTP API,即使只用低级套接字从头开始编写一个也不会太难。 如果您只想保护代码本身,您甚至不需要安全功能。
如果您只希望付费使用的人可以访问您的 API,那么您将需要某种身份验证,例如 API 密钥。 这也是一件非常容易的事情,并且可能几乎可以从上述某些框架中免费获得。
因此,您的客户端代码可能如下所示:
文件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
...
此代码执行 HTTP POST 请求,在请求正文中传递get_data_to_operate_on()
调用返回的任何数据。 响应的正文是处理的结果,在此代码中假定它是 JSON 数据,但也可以是其他格式。
您可以通过更改请求的路径('/somemagicfunction' 部分)或添加其他查询参数来添加各种选项。
这可能会帮助您开始使用服务器端: https : //nordicapis.com/8-open-source-frameworks-for-building-apis-in-python 。 这是托管 Python 服务器代码的一种方法: https : //devcenter.heroku.com/articles/getting-started-with-python
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.