A Python script running method and system

By compiling Python scripts into bytecode files and generating executable files at runtime, the problems of low running efficiency and easy leakage of Python scripts are solved, and efficient and secure script execution and protection are achieved.

CN114461310BActive Publication Date: 2025-07-25SHENZHEN LEAGSOFT TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210124518.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-02-10
Publication Date
2025-07-25
Estimated Expiration
2042-02-10

AI Technical Summary

Technical Problem

Python scripts are inefficient in running and easy to leak. The installation packages generated by existing packaging methods are large in size and difficult to protect products.

Method used

Compile Python script files into bytecode files, package them into installation packages, and generate executable files at runtime through the bytecode compiler and program loader, using signature verification and legality verification to ensure security and integrity.

Benefits of technology

Improves the execution efficiency of Python scripts, reduces the installation package size, enhances transmission security, and prevents script leakage and malicious modifications.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114461310B_ABST
    Figure CN114461310B_ABST
Patent Text Reader

Abstract

A method and system for running Python scripts provided by the present invention. The method includes obtaining a Python script file to be released, using a bytecode compiler to compile the Python script file to be released to obtain a bytecode file; packaging library files, bytecode files, bytecode compilers, and program loaders in a Python environment to obtain an installation package; when receiving a running instruction initiated for an installation package, releasing the library files, bytecode files, bytecode compilers, and program loaders in the installation package; using the released bytecode compiler to compile the bytecode file into an executable file; and using the released program loader to load the executable file and library files. This method improves the execution efficiency and solves the problem of Python script leakage.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of software systems, and particularly relates to a method and system for running Python scripts. Background Art

[0002] Currently, Python scripts are mainly run in the following way: after a Python script is debugged, an installation package is generated for release; after a user downloads and installs the installation package, all the dependent library files in the installation package and the written Python script are released; finally, the user runs the Python interpreter to interpret and execute the Python script to implement the corresponding function.

[0003] However, the current method for running Python scripts has the following defects:

[0004] 1. Python is a scripting language that needs to be interpreted and executed simultaneously, and each time a Python script is run, it needs to be interpreted, resulting in low execution efficiency.

[0005] 2. Python scripts are text files, and most of them are not encrypted files. Users can directly open and view the script content. Therefore, after a Python script is released, it is difficult to protect the product. For a small number of encrypted Python scripts, users can register hooks to decrypt the script and execute the Python script, but the Python script still has the problem of being easily leaked.

[0006] 3. Existing third-party Python script packaging methods can only simply read library files, compress the library files into zip files, and finally the generated installation package is too large in size. Summary of the Invention

[0007] Aiming at the defects in the prior art, the present invention provides a method and system for running Python scripts, which improves the execution efficiency and solves the problem of Python script leakage.

[0008] In a first aspect, a method for running a Python script includes:

[0009] Obtain a Python script file to be released, and compile the Python script file to be released using a bytecode compiler to obtain a bytecode file;

[0010] Package the library files, bytecode files, bytecode compiler, and program loader in the Python environment to obtain an installation package;

[0011] When a running instruction for an installation package is received, release the library files, bytecode files, bytecode compiler, and program loader in the installation package;

[0012] Compile the bytecode file into an executable file using the released bytecode compiler;

[0013] Load the executable file and library files using the released program loader.

[0014] Preferably, after obtaining the installation package, it further includes:

[0015] Release the installation package.

[0016] Preferably, when receiving a running instruction initiated for an installation package, releasing the library files, bytecode files, bytecode compiler, and program loader in the installation package specifically includes:

[0017] When receiving a running instruction initiated for an installation package, verify the signature of the installation package. When the signature verification passes, release the library files, bytecode files, bytecode compiler, and program loader in the installation package.

[0018] Preferably, using the released bytecode compiler to compile the bytecode file into an executable file specifically includes:

[0019] Run the released bytecode compiler;

[0020] Use the bytecode compiler to verify the legality of the released bytecode file;

[0021] After the legality verification passes, use the bytecode compiler to compile the bytecode file into an executable file.

[0022] Preferably, the library files in the Python environment are obtained through the following method:

[0023] Install the virtualenv component in the Python environment to create a working environment;

[0024] Define the files in the working environment as library files.

[0025] In a second aspect, a Python script running system includes:

[0026] A compilation unit: used to obtain the Python script file to be released, and use the bytecode compiler to compile the Python script file to be released to obtain a bytecode file;

[0027] A packaging unit: used to package the library files, bytecode files, bytecode compiler, and program loader in the Python environment to obtain an installation package;

[0028] Installation unit: When receiving a running instruction for an installation package, it releases the library files, bytecode files, bytecode compiler, and program loader in the installation package, and uses the released bytecode compiler to compile the bytecode files into executable files;

[0029] Running unit: It is used to load the executable files and library files by using the released program loader.

[0030] Preferably, it further includes:

[0031] Publishing unit: It is used to publish the installation package.

[0032] Preferably, the installation unit is specifically used for:

[0033] When receiving a running instruction for an installation package, it verifies the signature of the installation package. When the signature verification passes, it releases the library files, bytecode files, bytecode compiler, and program loader in the installation package.

[0034] Preferably, the installation unit is specifically used for:

[0035] Run the released bytecode compiler;

[0036] Use the bytecode compiler to verify the legality of the released bytecode files;

[0037] After the legality verification passes, use the bytecode compiler to compile the bytecode files into executable files.

[0038] Preferably, the library files in the Python environment are obtained through the following methods:

[0039] Install the virtualenv component in the Python environment and create a working environment;

[0040] Define the files in the working environment as library files.

[0041] As can be seen from the above technical solution, the Python script running method and system provided by the present invention convert the Python script file into a bytecode file for transmission. Since the bytecodes applicable to different operating systems are the same, the processing complexity of the packaging process and the bytecode compilation process is reduced. Only need to compile the Python script file once, and it can be used on all operating systems. And convert the Python script file into an executable file, so that when executing the Python script, the program loader can be directly used to load and execute it. The Python script does not need to be interpreted every time it runs, and the execution efficiency is high. And the whole process is transmitted through the bytecode file, and there is no Python script source code on the ground, which will not expose the business logic of the Python script, reducing the security of transmission, and solving the problem of Python script leakage existing in the prior art. And the bytecode file is not readable and cannot be maliciously modified or decompiled and restored to the Python script. Therefore, it can also avoid the problem that the program execution process is damaged due to others maliciously modifying the Python script. In addition, the executable file is a binary file and cannot be directly modified, which can also ensure the integrity of the program flow. Description of the Drawings

[0042] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following will briefly introduce the drawings required for use in the description of the specific embodiments or the prior art. In all the drawings, similar elements or parts are generally identified by similar reference numerals. In the drawings, the elements or parts do not necessarily draw according to the actual scale.

[0043] Figure 1 It is a flowchart of the Python script running method provided for the embodiment.

[0044] Figure 2 It is a flowchart of the executable file compilation method provided for the embodiment.

[0045] Figure 3 It is a module block diagram of the Python script running system provided for the embodiment. Detailed Embodiments

[0046] The following will combine the drawings to describe the embodiments of the technical solutions of the present invention in detail. The following embodiments are only used to illustrate the technical solutions of the present invention more clearly, so they are only examples and cannot be used to limit the protection scope of the present invention. It should be noted that unless otherwise specified, the technical terms or scientific terms used in this application should be the ordinary meanings understood by those skilled in the art to which the present invention belongs.

[0047] It should be understood that when used in this specification and the appended claims, the terms "comprising" and "including" indicate the presence of the described features, wholes, steps, operations, elements, and / or components, but do not preclude the presence or addition of one or more other features, wholes, steps, operations, elements, components, and / or their combinations.

[0048] It should also be understood that the terminology used in this specification of the present invention is for the purpose of describing particular embodiments only and is not intended to limit the present invention. As used in this specification of the present invention and the appended claims, unless the context clearly dictates otherwise, the singular forms "a", "an", and "the" are intended to include the plural forms.

[0049] As used in this specification and the appended claims, the term "if" can be interpreted, depending on the context, as "when", "once", "in response to determining", or "in response to detecting". Similarly, the phrases "if determined" or "if [the described condition or event] is detected" can be interpreted, depending on the context, as meaning "once determined", "in response to determining", "once [the described condition or event] is detected", or "in response to detecting [the described condition or event]".

[0050] Embodiment 1:

[0051] A method for running a Python script, see Figure 1 , comprising:

[0052] S1: Obtain the Python script file to be released, and use a bytecode compiler to compile the Python script file to be released to obtain a bytecode file;

[0053] S2: Package the library files, bytecode files, bytecode compiler, and program loader in the Python environment to obtain an installation package;

[0054] S3: When a running instruction for an installation package is received, release the library files, bytecode files, bytecode compiler, and program loader in the installation package;

[0055] S4: Use the released bytecode compiler to compile the bytecode file into an executable file;

[0056] S5: Use the released program loader to load the executable file and the library file.

[0057] In this embodiment, the Python script file to be released can be a script file that needs to be released and is available for other users to download or install. The Python script file to be released can be obtained by means of import. The bytecode compiler can be defined according to the format of the bytecode file. When the bytecode compiler compiles the Python script file, it first performs lexical analysis on the Python script file to be released and obtains the tokens of the Python script file to be released. Then it performs syntax analysis on the Python script file to obtain the AST structure, optimizes the AST structure, and obtains the bytecode file. The format of the bytecode file can be defined according to the Python script or the user's needs. In this way, even if the bytecode file is intercepted by others, they cannot decompile or decipher it because they do not know the format of the bytecode file, which improves the security of Python script transmission.

[0058] In this embodiment, after obtaining the bytecode file, the method packages the library file, the bytecode file, the bytecode compiler, and the program loader. The compression method used for packaging can be the zstd / lzma algorithm, which can further reduce the volume of the generated installation package and is more conducive to network transmission. The library file is a file on which the Python script file depends when it is executed. Since the formats of the bytecode files to be converted may be different for different Python scripts or users, the bytecode compiler also needs to be packaged simultaneously when packaging. The method also needs to package the program loader simultaneously. In this way, when the user runs the installation package, they do not need to download the program loader again and can directly use the released program loader to load the file, which is convenient for the user to use. The generated installation package format is different depending on the operating system being run. For example, the installation package suitable for the Windows operating system is an exe file, the installation package suitable for the Macos operating system is a dmg file, and the installation package suitable for the Linux operating system is a deb file.

[0059] In this embodiment, when a user wants to install an installation package, they can double-click the installation package to initiate a running instruction, which can release the library file, the bytecode file, the bytecode compiler, and the program loader in the installation package. The bytecode compiler is also used to compile the bytecode file into an executable file. The executable file obtained after compilation is different depending on the operating system being run. For example, the executable file in the Windows operating system can be a dll file, and the executable files in the Linux operating system and the Mac operating system can be so files.

[0060] This method converts a Python script file into a bytecode file for transmission. Since the bytecodes applicable to different operating systems are the same, this method reduces the processing complexity of the packaging process and the bytecode compilation process. This method only needs to compile the Python script file once and can be used on all operating systems. Moreover, the Python script file is converted into an executable file, so that when the Python script is executed, the program loader can directly load and execute it. The Python script does not need to be interpreted every time it runs, and the execution efficiency is high. And this method transmits the whole process through the bytecode file, without the Python script source code being stored on disk, which will not expose the business logic of the Python script and reduces the security risk during transmission, solving the problem of Python script leakage existing in the prior art. Also, the bytecode file is not readable and cannot be maliciously modified or decompiled back into a Python script, thus avoiding the problem that the program execution process is damaged due to malicious modification of the Python script by others. In addition, the executable file is a binary file and cannot be directly modified, which can also ensure the integrity of the program process.

[0061] Further, in some embodiments, after obtaining the installation package, it further includes:

[0062] Release the installation package.

[0063] In this embodiment, when the method obtains the installation package, it can release the installation package on various software download platforms for other users to download. The installation package is released through the bytecode file and is not easily deciphered or decompiled, solving the problem in the prior art that it is difficult to protect the product after the Python script is released.

[0064] Further, in some embodiments, when receiving a running instruction for an installation package, releasing the library files, bytecode files, bytecode compiler, and program loader in the installation package specifically includes:

[0065] When receiving a running instruction for an installation package, verify the signature of the installation package. When the signature verification passes, release the library files, bytecode files, bytecode compiler, and program loader in the installation package.

[0066] In this embodiment, when the method runs the installation package, it also needs to verify the signature of the installation package to confirm whether the installation package has been modified. If the signature verification is successful, it means that the installation package has not been modified. At this time, release the library files, bytecode files, bytecode compiler, and program loader in the installation package. Specifically, when implementing, the method can sign the installation package when the installation package is released to obtain the original signature of the installation package. When running the installation package, read the signature of the installation package and determine whether the signature is consistent with the original signature. If they are consistent, it means that the signature verification passes.

[0067] Further, in some embodiments, referring to Figure 2 , using the released bytecode compiler to compile the bytecode file into an executable file specifically includes:

[0068] S11: Run the released bytecode compiler;

[0069] S12: Use the bytecode compiler to verify the legality of the released bytecode file;

[0070] S13: When the legality verification passes, use the bytecode compiler to compile the bytecode file into an executable file.

[0071] In this embodiment, after the library file, bytecode file, bytecode compiler, and program loader in the installation package are released, the bytecode compiler is run, and the legality of the released bytecode file is verified using the bytecode compiler. Only when the bytecode file is legal, the bytecode file in the installation package is compiled into an executable file. The legality verification is determined according to Python scripts or user requirements, such as verifying whether the data exceeds the limit and whether the data format meets the requirements, etc. When the bytecode file is being compiled, the bytecode file can first be interpreted, converted into LLVM IR, then the LLVM IR is compiled to obtain assembly code, and the assembly code is converted into an LLVM link to obtain an executable file.

[0072] Further, in some embodiments, the library file in the Python environment is obtained by the following method:

[0073] Install the virtualenv component in the Python environment and create a working environment;

[0074] Define the files in the working environment as library files.

[0075] In this embodiment, the method can first install the virtualenv component in the Python environment to create a working environment, so that the files in this working environment can be used as the library files on which the Python script depends when executed.

[0076] Embodiment 2:

[0077] A Python script running system, referring to Figure 3 , includes:

[0078] Compilation unit 1: Used to obtain the Python script file to be released and compile the Python script file to be released using the bytecode compiler to obtain a bytecode file;

[0079] Packaging Unit 2: Used to package library files, bytecode files, bytecode compilers, and program loaders in a Python environment to obtain an installation package;

[0080] Installation Unit 3: When receiving a running instruction for an installation package, it releases the library files, bytecode files, bytecode compilers, and program loaders in the installation package, and uses the released bytecode compiler to compile the bytecode files into executable files;

[0081] Running Unit 4: Used to load the executable files and library files using the released program loader.

[0082] Preferably, it further includes:

[0083] Release Unit: Used to release the installation package.

[0084] Preferably, Installation Unit 3 is specifically used for:

[0085] When receiving a running instruction for an installation package, it verifies the signature of the installation package. When the signature verification passes, it releases the library files, bytecode files, bytecode compilers, and program loaders in the installation package.

[0086] Preferably, Installation Unit 3 is specifically used for:

[0087] Run the released bytecode compiler;

[0088] Use the bytecode compiler to verify the legality of the released bytecode files;

[0089] After the legality verification passes, use the bytecode compiler to compile the bytecode files into executable files.

[0090] Preferably, the library files in the Python environment are obtained through the following methods:

[0091] Install the virtualenv component in the Python environment to create a working environment;

[0092] Define the files in the working environment as library files.

[0093] For the system provided by the embodiments of the present invention, for the sake of brief description, for the parts not mentioned in the embodiment part, reference may be made to the corresponding content in the foregoing embodiments.

[0094] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements for some or all of the technical features; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention, and they should all be covered within the scope of the claims and the description of the present invention.

Claims

1. A method for running a Python script, characterized in that, including: Obtain the Python script file to be released, and use a bytecode compiler to compile the Python script file to be released to obtain a bytecode file; Package the library files, the bytecode file, the bytecode compiler, and the program loader in the Python environment to obtain an installation package; When receiving a running instruction for one of the installation packages, release the library files, the bytecode file, the bytecode compiler, and the program loader in the installation package; Use the released bytecode compiler to compile the bytecode file into an executable file; Use the released program loader to load the executable file and the library files; When the bytecode compiler compiles a Python script file, it first performs lexical analysis on the Python script file to be released and obtains the tokens Token of the Python script file to be released. Then it performs syntax analysis on the Python script file to obtain an AST structure, and optimizes the AST structure to obtain a bytecode file; The format of the bytecode file is defined according to the Python script or the user's requirements; After obtaining the installation package, it further includes: Release the installation package; When the bytecode file is compiled, it can first interpret the bytecode file, convert it to LLVM IR, then compile the LLVM IR to obtain assembly code, and convert the assembly code to an LLVM link to obtain an executable file; The library files in the Python environment are obtained through the following method: Install the virtualenv component in the Python environment and create a working environment; Define the files in the working environment as the library files.

2. The Python script running method according to claim 1, wherein The step of when receiving a running instruction for one of the installation packages and releasing the library files, the bytecode file, the bytecode compiler, and the program loader in the installation package specifically includes: When receiving a running instruction for an installation package, verify the signature of the installation package. When the signature verification passes, release the library files, the bytecode file, the bytecode compiler, and the program loader in the installation package.

3. The Python script running method according to claim 1, characterized in that, The step of using the released bytecode compiler to compile the bytecode file into an executable file specifically includes: Run the released bytecode compiler; Use the bytecode compiler to verify the legality of the released bytecode file; When the legality verification passes, use the bytecode compiler to compile the bytecode file into the executable file.

4. A Python script running system, characterized in that, including: Compilation unit: used to obtain the Python script file to be released, and use a bytecode compiler to compile the Python script file to be released to obtain a bytecode file; Packaging unit: used to package the library files, the bytecode file, the bytecode compiler, and the program loader in the Python environment to obtain an installation package; Installation unit: When receiving a running instruction for a said installation package, to release the library file, the bytecode file, the bytecode compiler, and the program loader in the installation package, and use the released bytecode compiler to compile the bytecode file into an executable file; Running unit: To use the released program loader to load the executable file and the library file; When the bytecode compiler compiles a Python script file, it first performs lexical analysis on the Python script file to be released and obtains the tokens Token of the Python script file to be released. Then it performs syntax analysis on the Python script file to obtain the AST structure, and optimizes the AST structure to obtain the bytecode file; The format of the bytecode file is defined according to the Python script or the user's requirements; It also includes: Release unit: To release the installation package; When the bytecode file is being compiled, it can first interpret the bytecode file, convert it to LLVM IR, then compile the LLVM IR to obtain assembly code, and convert the assembly code to an LLVM link to obtain an executable file; The library file in the said Python environment is obtained by the following method: Install the virtualenv component in the said Python environment to create a working environment; Define the files in the working environment as the library files.

5. The Python script running system according to claim 4, characterized in that The said installation unit is specifically used for: When receiving a running instruction for an installation package, to verify the signature of the installation package. When the signature verification passes, release the library file, the bytecode file, the bytecode compiler, and the program loader in the installation package.

6. The Python script running system according to claim 5, characterized in that, The said installation unit is specifically used for: Run the released bytecode compiler; Use the bytecode compiler to verify the legality of the released bytecode file; After the legality verification passes, use the bytecode compiler to compile the bytecode file into the executable file.