Receive data from GET request

Asked by fordox

Hello.
Is it possible for Sikuli to do the following:

1. Sikuli script makes a request to page: http://example.com/get.php
2. get.php sends us back GET request:

vr=50-100

the GET request result is always different

3. When sikuli takes each variable from the received request and uses it, substituting the values. When I use it in the following code:
...
        if now.hour >= 0 and now.hour < 1: # 00:00 - 01:00
            # send request to http://example.com/get.php and receive a reply
            # If from http://example.com/get.php no response is received - to try to get it again
            mytime = random.randint (50, 100) # received from the GET request - vr
        if now.hour >= 1 and now.hour < 2: # 01:00 - 02:00
            # send request to http://example.com/get.php and receive a reply
            # If from http://example.com/get.php no response is received - to try to get it again
            mytime = random.randint (100, 150) # received from the GET request - vr
        if now.hour >= 2 and now.hour < 3: # 02:00 - 03:00
            # send request to http://example.com/get.php and receive a reply
            # If from http://example.com/get.php no response is received - to try to get it again
            mytime = random.randint (200, 300) # received from the GET request - vr

        wait (mytime)
...

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
fordox
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

if I understand right, you have to look into the Python module
https://docs.python.org/2.6/library/urllib.html

Revision history for this message
fordox (neveroid) said :
#2

I understand how to send the GET-request, but how to convert received data back into a variable - is unclear.

import urllib
f = urllib.urlopen("http://example.com/get.php?%s") # send request to http://example.com/get.php and receive a reply

Then I get a response like this: http://example.com/get.php?mytime=50,100&vr=4

the question is how to assign the value for the variable "mytime" and "work"?

...
mytime = random.randint (50, 100) # received from the GET request
vr = 4
...

Revision history for this message
RaiMan (raimund-hocke) said :
#3

the docs for urllib.urlopen say:

...
 If all went well, a file-like object is returned. This supports the following methods: read(), readline(), readlines(), fileno(), close(), info(), getcode() and geturl(). It also has proper support for the iterator protocol.

so
print f.read()

should show some content.

Revision history for this message
fordox (neveroid) said :
#4

did like this:

import random
import urllib
import re

getval = urllib.urlopen("http://example.com/get.php?%s")
a = getval.read()
b = re.search('delay=(.*?)type=(.*?)$', a)
c = b.group(1)
d = b.group(2)

if (c.find(",")==-1):
 time = eval("random.randint("+c+","+c+")");
else:
 time = eval("random.randint("+c+")");
var = int(d)

print time, var