Developers' Forum

Use urrlib in python template

Picture of Pascal Veron
Use urrlib in python template
by Pascal Veron - Friday, 17 March 2017, 11:38 PM
Hi everyone,

in my custom template, i need to use the urllib.request module in order to fetch some datas on a web site. However when i execute  in debug mode my code, i obtain :

File "/usr/lib/python3.4/", line 503, in create_connection
ConnectionRefusedError: [Errno 111] Connection refused

Does someone knows it if possible to make http connection through CodeRunner ?

Picture of Richard Lobb
Re: Use urrlib in python template
by Richard Lobb - Saturday, 18 March 2017, 1:11 PM

Hi Pascal

The Jobe server is normally firewalled to block outgoing socket connections. You (or whoever administers the Jobe server) would have to poke a hole in the firewall to allow jobs running on Jobe to access specific sites. Other than that, I would expect it to work OK - let us know if it doesn't.


Picture of Pascal Veron
Re: Use urrlib in python template
by Pascal Veron - Saturday, 18 March 2017, 9:24 PM

Thanks Richard, i should have carefully read all the coderunner documentation and jobe doc, i've seen that it is explained that some firewall rules are applied .......

Could you help me for the following problem :

I'm  always searching to generate random values in questions. I have found a trick. In the question form, i insert an iframe which makes a call to a php program on a server which generates the random values. 

Let's take a simple example, the php program generates the question :

What is the value of a modulo m for a=15 and m =7. Fill the answer in the variable r.

Now in the answer box preload, i put




and the student have to fill a and m with the datas of the question and gives the answer in r.

Then i can check in my customize template that r = a modulo m.

The main problem with this is that the student can give any input (for example a=1 and m=2) as soon as the value r is correct, that is to say that i cannot verify that the student answers the question with the data randomly generated by my php script.....

Could you tell me where i have to modify the coderunner source code so that :

1) when authoring a question, there is a new text field which is automatically filled wit a random hexadecimal string (it will be used as an encryption key K).

2) inside my customized template i can use the value of K

My idea is the following :  When i call my php program, i also send the generated K as a parameter, so that my program sends back the datas a , m and  a token tok encrypted from the values of a, m and K.

In the answer the student should specify the value of a , m , tok and his answer. Now in my customize template, i check from the values a and m given by the student and the value K generated in the input field when authoring the question that the corresponding encrypting value is equal to tok, which mean that the student has used the good datas.

Hope that my poor english is sufficiently clear.


Picture of Pascal Veron
Re: Use urrlib in python template
by Pascal Veron - Saturday, 18 March 2017, 10:50 PM

Me again ! Forgot my last message, it does not solve my problem. In fact, here is what i really need :

When the student obtains the html code of the question form , i need a hidden input which contains the value of a randomly generated key K, so that in JavaScript i can access this value and sends it to my php script which will sends back the body of my iframe with the text of the answer and the value of the computed token from (taking the previous example), the values of a ,m and K. 

Moreover in my customize template , i need to be able to access this randomly generated K (may be via QUESTION.questiontext ??)

Or may be there is a simple alternative, are there anyway to obtain the value of QUESTION.questionid (i guess it is a unique number ) when i use JavaScript in the text of my question ?

Thanks again.

Picture of Richard Lobb
Re: Use urrlib in python template
by Richard Lobb - Sunday, 19 March 2017, 9:08 PM

I don't really have an easy answer here. The questionid isn't helpful, as it'll be the same for all attempts by all students. You really want to get the ID of the particular question attempt, which will be constant only for the different submissions a student makes during a single attempt on the question. However, I can't see any easy way to get hold of that.

If you're working with your own fork of coderunner (specifically question.php), probably the best way to get a unique key for all steps in a particular question attempt is to override the start_attempt method of the base question type - you could create a long hash string and store it as what's called a "qt_var" (question-type variable). That would then be available in all calls to grade_response, and can be inserted into the Twig environment, making it available to the question template.

Having an iframe + JavaScript + PHP script to customise the question text itself is very cunning but it does seem rather complicated. David Bowes approach of passing the question text through Twig, with the same environment as is used for the template, seems like a nicer way to go.

I do intend looking into incorporating some such question-randomisation capability into the CodeRunner master, but it won't happen until the end of the year.


Tim at Lone Pine Koala Sanctuary
Re: Use urrlib in python template
by Tim Hunt - Monday, 20 March 2017, 1:48 AM

If you are hacking the code, then there is a proper way to handle this within the Moodle question framework.

It is known as question variants. To start using it, you need to override this method in the question class:

Then, when an attempt at a question is started ( you get passed which varian number of the question to display. All the randomisation you do within the question should be seeded on that.

There are various reasons for doing it this way:

  • You don't have to worry about doing the randomisation (or pseudo-randomisation). That handles things like ensuring that student's don't get the same variant again until they have seen them all once.
  • The question statistics let you drill down to see an analysis by variant, which lets you see whether, for example, students are finding one random variant harder or easier than the rest on average. If they are, then you probably want to look into that.

The question type with the most sophisticated randomisation that I know is STACK. However, it is not fundamentally complicated. It might be worth looking at what STACK does with its notion of 'Deployed variants'. That seems to be a useful way to manage things.

Picture of Pascal Veron
Re: Use urrlib in python template
by Pascal Veron - Monday, 20 March 2017, 11:39 PM

Thanks for all your answers. I will try to find a solution.



Picture of David Bowes
Re: Use urrlib in python template
by David Bowes - Monday, 3 April 2017, 9:07 PM

I have a variant of coderunner which does override the initialisation of variables. 

There is also a feature to allow you to display your own data entry rather than using the defualt mechanism.  This may allow you to do what you want.

NB see reply to variable question.