creating a variable from a function

Bengt Richter bokr at oz.net
Sun Oct 12 08:02:26 EDT 2003


On 9 Oct 2003 07:38:53 -0700, steve.yeoman at jacobsrimell.com (steve) wrote:

>[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 at 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()
>
>[/CODE]
>
>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




More information about the Python-list mailing list