-1

I want to create a python script so that it can only be used 5 times after that python script deleted so as not to be misused...

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Ardi S
  • 9
  • 2
    you would have to use a batch/bash/powershell script depending on your OS and use that to call your `.py` file five times and then delete it afterwards –  Jun 27 '18 at 01:51
  • 1
    this is called via http / command line / other way? – Itay Moav -Malimovka Jun 27 '18 at 01:58
  • 1
    There isn't a way to do this in a way that people with any amount of python knowledge won't be able to easily circumvent. To make this question possible - can we assume that people aren't going to open your script in a text editor? – Shadow Jun 27 '18 at 02:05
  • 1
    What sort of "misuse" are you expecting? This may be the wrong approach. What's to stop someone from making a copy of the script and using it ad infinitum, or merely marking the script read-only? – OregonJim Jun 27 '18 at 02:21

3 Answers3

1

Here you go:

counter=0
import os
import fileinput

filename = os.path.abspath(__file__)
if(counter >= 5):
    os.remove(filename)
else:
    f = open(filename)
    lines = f.read().splitlines()
    f.close()
    line = lines[0].split("=")
    line[1]=str(counter+1)
    lines[0]='='.join(line)
    open(filename, 'w').write('\n'.join(lines))
    # Do whatever the script should do here

But as mentioned in comments above, there is no way to do this that cannot be easily bypassed by anyone with a text editor or other very simple means.

klutt
  • 30,332
  • 17
  • 55
  • 95
1

Require an online service to validate and decrypt the code to be run.

This is pretty much the only way that cannot be bypassed by the most trivial of python / operating system knowledge. It's also the only way that's likely to work on windows, which holds a lock on an open file which would prevent your script from being able to delete itself.

Here is a rough guide as to how you could achieve this:

  • Write your program. Do not distribute it.
  • Write a webserver.
    • This webserver would take a POST (for example) with a serial number.
    • It would then look up that serial number in some from of database, where the times that serial number has been used will be logged.
    • If that serial number has not been used too many times - return a decryption key.
  • Write the program you distribute.
    • This would contain a shim to hit the webserver with a specific serial number.
    • Then it would use the key returned from the webserver to decrypt your code, then run it.

Although this method would work, it still has drawbacks;

  • It requires you to create a new distribution for every single person who wants to run it.
  • It has a hard dependency on a working network connection, and an active server.
  • If you aren't using a secure connection, the key could be sniffed.
  • Every decryption is logged, rather than every successful execution. Depending on the nature of your program, this could be a drawback as unsuccessful attempts to run the program will still count towards their usage limit.
  • And the biggest problem: If the user edited your shim to simply print the serial number before it had expired, they could simply skip your shim and provide the key directly.

Basically - you can't do this successfully in python. However by doing the above, you heighten the chances of people not realising they have been locked out of your program until it is too late. Once the webserver stops handing out keys - that's it.

Shadow
  • 8,749
  • 4
  • 47
  • 57
0

Add to the script:

import os
with open("file.txt","a") as file:
    file.write(1)
with open("file.txt","r") as file:
    if len(file.read())>5:
        os.system("rm <path to this file>")

Given @Shadow's assumption

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
  • `os.unlink(__file__)` would be a cleaner way to remove the file - it doesn't make assumptions about location or operating system. – Shadow Jun 27 '18 at 02:40