从原始链接加载并执行完整的 python 脚本?

发布于 2025-01-13 04:57:03 字数 1272 浏览 5 评论 0原文

我在尝试从 Pastebin/github 页面加载完整的 Python 脚本时遇到一些问题。

我按照此链接,尝试将原始文件转换为临时文件并像模块一样使用它: 如何从原始链接(例如 Pastebin)加载 python 脚本?

这是我的测试(使用一个非常简单的 python 脚本作为原始,我的主程序不是这样不幸的是,很简单): https://trinket.io/python/0e95ba50c8

当我运行脚本时(现在正在 .py 文件的当前目录中创建一个临时文件)我收到此错误:

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\BOT\\Images\\tempxm4xpwpz.py'

否则我也使用 exec() 函数...不幸的是没有更好的结果。 使用这段代码:

import requests as rq
import urllib.request

def main():
    code = "https://pastebin.com/raw/MJmYEKqh"
    response = urllib.request.urlopen(code)
    data = response.read()
    exec(data)

我收到此错误:

  File "<string>", line 10, in <module>
  File "<string>", line 5, in hola
NameError: name 'printest' is not defined

由于我的程序与这个简单的测试相比更复杂,我不知道如何继续...... 基本上我想要实现的是在 GitHub 上编写程序的完整脚本并将其连接到 .exe,这样如果我升级原始版本,我的程序也会更新。避免每次生成和共享(仅与我的朋友)新的 .exe...

您认为可能吗?如果是这样..我做错了什么?

PS:我也愿意接受其他可能性,让我的朋友更新程序,而无需每次下载 .exe,只要他们不需要安装任何东西(这就是我使用 .exe 的原因)。

I'm facing some problems trying to load a full python script from my pastebin/github pages.

I followed this link, trying to convert the raw into a temp file and use it like a module: How to load a python script from a raw link (such as Pastebin)?

And this is my test (Using a really simple python script as raw, my main program is not so simple unfortunately): https://trinket.io/python/0e95ba50c8

When I run the script (that now is creating a temp file in the current directory of the .py file) I get this error:

PermissionError: [Errno 13] Permission denied: 'C:\\Users\\BOT\\Images\\tempxm4xpwpz.py'

Otherwise I also treid the exec() function... No better results unfortunately.
With this code:

import requests as rq
import urllib.request

def main():
    code = "https://pastebin.com/raw/MJmYEKqh"
    response = urllib.request.urlopen(code)
    data = response.read()
    exec(data)

I get this error:

  File "<string>", line 10, in <module>
  File "<string>", line 5, in hola
NameError: name 'printest' is not defined

Since my program is more complex compared to this simple test, I don't know how to proceed...
Basically What I want to achieve is to write the full script of my program on GitHub and connect it to a .exe so if I upgrade the raw also my program is updated. Avoiding to generate and share (only with my friends) a new .exe everytime...

Do you think is possible? If so.. what am I doing wrong?

PS: I'm also open to other possibilities to let my friends update the program without downloading everytime the .exe, as soon as they don't have to install anything (that's why I'm using .exe).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2025-01-20 04:57:03

免责声明:运行未经验证(更不用说不受信任)的代码确实不是一个好主意。话虽如此,如果您真的想这样做......

可能是最简单且“最不肮脏”的方法将运行全新的流程。这可以直接在 python 中完成。像这样的东西应该可以工作(灵感来自您在问题中链接的答案):

import urllib.request
import tempfile
import subprocess

code = "https://pastebin.com/raw/MJmYEKqh"
response = urllib.request.urlopen(code)
data = response.read()

with tempfile.NamedTemporaryFile(suffix='.py') as source_code_file:
    source_code_file.write(data)
    source_code_file.flush()
    subprocess.run(['python3', source_code_file.name])

您还可以使用 exec 使代码正确运行:

什么可能工作:< /strong>

  • exec(data, {}) -- 您所需要做的就是提供 {} 作为第二个参数(即使用 exec(数据,{}))。函数 exec 可以接收两个额外的可选参数——全局变量和局部变量。如果您只提供一个,它将为当地人使用相同的目录。也就是说,exec 中的代码的行为类似于顶层的“干净”环境。这就是您的目标。

  • exec(data, globals()) -- 第二个选项是从当前作用域提供全局变量。这也将起作用,尽管您可能不需要为执行代码提供对全局变量的访问权限,因为该代码无论如何都会设置内部的所有内容

什么不起作用:

  • exec(data, {}, {}) -- 在这种情况下,执行的代码将有两个不同的字典(尽管都是空的)用于局部变量和全局变量。因此,它的行为将是“as-in”(我不太确定这部分,但当我测试它时,它是这样的)功能。这意味着它将把 printesthola 函数添加到本地作用域而不是全局作用域。不管怎样,我希望它能够工作——我希望它只会从本地范围而不是全局范围查询 hola 函数中的 printest 。但是,由于某种原因,本例中的 hola 函数以期望 printest 位于全局范围而不是本地范围的方式进行编译,但事实并非如此。我实在不明白为什么。所以这将导致 NameError

  • exec(data, globals(), locals()) -- 这将提供对调用函数的状态的访问。然而,它会因与前一种情况相同的原因而崩溃

  • exec(data)——这只是exec(data, globals(), locals( )

Disclaimer: it is really not a good idea to run an unverified (let alone untrusted) code. That being said if you really want to do it...

Probably the easiest and "least-dirty" way would be to run whole new process. This can be done directly in python. Something like this should work (inspiration from the answer you linked in your question):

import urllib.request
import tempfile
import subprocess

code = "https://pastebin.com/raw/MJmYEKqh"
response = urllib.request.urlopen(code)
data = response.read()

with tempfile.NamedTemporaryFile(suffix='.py') as source_code_file:
    source_code_file.write(data)
    source_code_file.flush()
    subprocess.run(['python3', source_code_file.name])

You can also make your code with exec run correctly:

What may work:

  • exec(data, {}) -- All you need to do, is to supply {} as second argument (that is use exec(data, {})). Function exec may receive two additional optional arguments -- globals and locals. If you supply just one, it will use the same directory for locals. That is the code within the exec would behave like sort-of "clean" environment, at the top-level. Which is something you aim for.

  • exec(data, globals()) -- Second option is to supply the globals from your current scope. This will also work, though you probably has no need to give the execucted code access to your globals, given that that code will set-up everything inside anyway

What does not work:

  • exec(data, {}, {}) -- In this case the executed code will have two different dictionaries (albeit both empty) for locals and globals. As such it will behavie "as-in" (I'm not really sure about this part, but as I tested it, it seams as such) the function. Meaning that it will add the printest and hola functions to the local scope instead of global scope. Regardless, I expected it to work -- I expected it will just query the printest in the hola function from the local scope instead of global. However, for some reason the hola function in this case gets compiled in such a way it expects printest to be in global scope and not local, which is not there. I really did not figured out why. So this will result in the NameError

  • exec(data, globals(), locals()) -- This will provide access to the state from the caller function. Nevertheless, it will crash for the very same reason as in the previous case

  • exec(data) -- This is just a shorthand for exec(data, globals(), locals()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文