creating a variable from a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • steve

    creating a variable from a function

    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.
  • Bengt Richter

    #2
    Re: creating a variable from a function

    On 9 Oct 2003 07:38:53 -0700, steve.yeoman@ja cobsrimell.com (steve) wrote:
    [color=blue]
    >
    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[/color]
    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?
    [color=blue]
    >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[/color]
    I guess it might mean it, for the context it's running in.
    [color=blue]
    >
    >(note: the 3rd and 4th line of page(1) are actually one line)
    >
    >Any ideas anyone? thanks.[/color]

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

    Regards,
    Bengt Richter

    Comment

    Working...