0
for i in xrange(1,NUM_USERS+1):
            print i
            private = RSA.generate(3072,Random.new().read)
            public = private.publickey()
            new_user = User(public_rsa=public.exportKey(), secret_rsa=private.exportKey())
            new_user.save()

In the above loop, I have given the value of NUM_USERS=100 but the loop is iterating till 200 instead of 100. What might be the possible reason for this ?

EDIT: I am so sorry guys, I accidentally figured out that the whole python method is being called twice, I don't know why though, so I will describe in detail. I am writing a django based server side, which has methods as following:

def index(request):
    return HttpResponse("CREST Top Dir: " + PROJECT_ROOT)

def server_setup(request):
    try:
        process = subprocess.check_output(BACKEND+"mainbgw setup " + str(NUM_USERS), shell=True,\
                                          stderr=subprocess.STDOUT)

        for i in xrange(1,NUM_USERS+1):
            print i

Now what happens is when I call the server_setup view sometimes it executes more than once. Similarly if I call index view sometimes server_setup is also called in simultaneously. So the problem is not with xrange but with method calling. What could be the reason for this problem ?

bawejakunal
  • 1,678
  • 2
  • 25
  • 54
  • Let me guess, `new_user = User(...)` or `new_user.save()` increases `NUM_USERS`. – 101 Jul 22 '15 at 03:42

1 Answers1

1

Check if NUM_USERS is 100.

for i in xrange(1,NUM_USERS+1):

    print 'NUM_USERS:', NUM_USERS  # check it

    print i
    private = RSA.generate(3072,Random.new().read)
    public = private.publickey()
    new_user = User(public_rsa=public.exportKey(), secret_rsa=private.exportKey())
    new_user.save()
letiantian
  • 437
  • 2
  • 14
  • I have hardcoded `NUM_USERS=100` so yeah for sure it is 100. The loop runs prefectly sometimes but most of the times it might just overshoot. – bawejakunal Jul 22 '15 at 03:36
  • 1
    Try this answer out. The only way this is possible is if `NUM_USERS` isn't 100 to start with _or_ it is modified within your loop. This code will check both for you. – 101 Jul 22 '15 at 03:41