creating a variable from a function

S

steve

Code:
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from HTTPClient import NVPair

log = grinder.logger.output

# Random Number Generator
def randomnumber():
	import random
	number = random.randrange(1, 2000, 1)
	return number

# We declare a default URL for the HTTPRequest.
request = HTTPRequest(url = "http://host")

# Details of HTTP request
def page1():
	request.GET('/app')
	request.GET('/app/j_security_check?j_password=jr&j_username=operator@DIC')
	request.GET('/app/adduser.do?cn=jbloggs_%(seed)s&userPassword=jbloggs'%
{'seed': randomnumber()} )

page1Test = Test(1, "First page").wrap(page1)

class TestRunner:
    def __call__(self):
        page1Test()

I can't get the output from randomnumber() to input as the variable
%(seed)s in page1(). randomnumber() works ok on its own, but when it
is called it seems to be inserting the actual function into the
variable, rather than the output.

I get this error when running it:
ImportError: no module named random

(note: the 3rd and 4th line of page(1) are actually one line)

Any ideas anyone? thanks.
 
B

Bengt Richter

Code:
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from HTTPClient import NVPair

log = grinder.logger.output

# Random Number Generator
def randomnumber():
	import random
	number = random.randrange(1, 2000, 1)
	return number

# We declare a default URL for the HTTPRequest.
request = HTTPRequest(url = "http://host")

# Details of HTTP request
def page1():
	request.GET('/app')
	request.GET('/app/j_security_check?j_password=jr&j_username=operator@DIC')
	request.GET('/app/adduser.do?cn=jbloggs_%(seed)s&userPassword=jbloggs'%
{'seed': randomnumber()} )

page1Test = Test(1, "First page").wrap(page1)

class TestRunner:
def __call__(self):
page1Test()

I can't get the output from randomnumber() to input as the variable
%(seed)s in page1(). randomnumber() works ok on its own, but when it
Are you running on your PC when it works, and having it run on a server
when it doesn't? Perhaps the environment on the server is not set up
the same, so that random is not found?

What happens if you run page1() manually and set request to something that will print
the GET args passed to it, e.g., if you do this (untested)

class Foo(object):
def GET(self, *args): print 'GET args = %r' % args
request = Foo()
page1()

to override request, interactively after the above, what do you get?
is called it seems to be inserting the actual function into the
variable, rather than the output.

I get this error when running it:
ImportError: no module named random
I guess it might mean it, for the context it's running in.
(note: the 3rd and 4th line of page(1) are actually one line)

Any ideas anyone? thanks.

Try putting debug prints or log writes in your code to tell you more.

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top