John Davidson

Sending data from LMS Moodle (php) to python CGI script and get it back

0 comments
Message:


I installed Learning Managment System (LMS) 'Moodle' to XAMPP local server. And there is instrument for file downloading, called "filepicker".


I want to pass text, received from 'filepicker' instrument (uploading .txt files only) from LMS Moodle to python-CGI script, process it, get the processed data back, and echo it on a page.


Using:


OS - Windows 10.


Local server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.28


python 3.10.4


I made var_dump($content); of uploaded file, So it is definitely a string:


string(828) "..........my text here............"


Also I clearly know, that my CGI script work if i manually input data in it,like:


http://localhost/speciallocation/local/tokenize/morgot.py?someamountoftext=Enter your text here


Output: ['Enter', 'your', 'text', 'here']


But when I press submit button, I only get the name of my file, since I don't transmit it to CGI, but simply echo it.


If just echo content of file, It also works. It brings me to think, that there is something wrong with send&get data part.......


Any thoughts people? Maybe I am doing somethig wrong at the very basics? Like POST vs GET method? Or something else? Please, help!


My php code:


<?php

require_once(DIR . '/../../config.php');
require_once($CFG->dirroot . '/local/tokenize/classes/forms/tokenization.php');
$PAGE->set_url(new moodle_url('/local/tokenize/tokenization.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title(get_string('TOKENIZATOR', 'local_tokenize'));

$mform= new tokenization();
echo $OUTPUT->header();

if ($mform->is_cancelled()) {
//Handle form cancel operation, if cancel button is present on form
} else if ($fromform = $mform->get_data()) {
//In this case you process validated data. $mform->get_data() returns data posted in form.

$name = $mform->get_new_filename('userfile');
echo $name. '<br>';
$content = $mform->get_file_content('userfile');
//echo $content;
var_dump($content);

$morgot_link = "http://localhost/diplom/local/tokenize/morgot.py?someamountoftext=" . $content;
$morgot_data = file_get_contents($morgot_link);
echo $morgot_data;

} else {
// this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
// or on the first display of the form.

//displays the form
$mform->display();
}

echo $OUTPUT->footer();


My CGI python code:

#!C:\Users\HP\AppData\Local\Programs\Python\Python310-32\python.exe


import os
import urllib.parse
import nltk

query_dict = urllib.parse.parse_qs(os.environ['QUERY_STRING'])
input_something = str(query_dict['someamountoftext'])[2: -2]
def tknz_wrd(someamountoftext):
return(nltk.word_tokenize(someamountoftext))


print("Content-Type: text/html\n")

print (tknz_wrd(input_something))

morgot.py - the name of my CGI python file.


Addition: I checked if the $content of file is being placed into $morgot_link:


$morgot_link = "http://localhost/diplom/local/tokenize/morgot.py?someamountoftext=" . $content;
echo $morgot_link;

Yes, the output for this is correct:


http://localhost/diplom/local/tokenize/morgot.py?someamountoftext=...........many text here.............


It pushes me to think, that the problem is with receiving part (But I don't completely deny probability with sending part issue). Also I think there could be some kinds of restrictions\permissions in Moodle to recieve data like that.


Useful links:


I followed these instructions for CGI-python script:


step 1) Run Python Programs as Web Applications locally on your Machine with Xampp - https://www.youtube.com/watch?v=cFAcFP3Di6s


step 2) Build a Web API with Python - https://www.youtube.com/watch?v=Bdeclymkt-A


step 3) Call Python (Web API) from PHP - https://www.youtube.com/watch?v=Bx_BEA8VPq0


Moodle installation:


How to install Moodle eLearning in localhost (XAMPP) on Windows - https://www.youtube.com/watch?v=My5DzB874_o


P.S. I also tried curl, replacing $morgot_data = file_get_contents($morgot_link); like this:


$postfields = $content;
$url="http://localhost/diplom/local/tokenize/morgot.py?someamountoftext=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$result = curl_exec($ch);
echo $result;

Did not help.
Sorry for bad English.



My Answer:
"Assignment". When a user downloads a file, I want to send some data (like user ID, file name, etc.) from Moodle (which is written in PHP) to a Python CGI script, process the data in the script, and then send the processed data back to Moodle.

To achieve this, you can use the following steps:

1. Create a Python CGI script that will receive the data from Moodle and process it. Here is an example of a simple Python CGI script that receives data from a POST request:

python
#!/usr/bin/env python

import cgi
import cgitb

cgitb.enable()

print("Content-Type: text/html\n")

form = cgi.FieldStorage()
user_id = form.getvalue('user_id')
file_name = form.getvalue('file_name')

# Process the data here
processed_data = "Processed data for user {} and file {}".format(user_id, file_name)

print(processed_data)


Save this script as `process_data.py` in your XAMPP server's `cgi-bin` directory.

2. In your Moodle PHP code, you can use the `curl` function to send the data to the Python CGI script. Here is an example of how you can do this:

php
$user_id = 123;
$file_name = 'example.txt';

$data = array('user_id' => $user_id, 'file_name' => $file_name);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/cgi-bin/process_data.py');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

echo $response;


3. Make sure that the `curl` extension is enabled in your PHP configuration.

4. When a user downloads a file in Moodle, call the PHP code that sends the data to the Python CGI script. You can do this by adding the PHP code to the file download process in Moodle.

By following these steps, you can send data from Moodle to a Python CGI script and get the processed data back.

Rate this post

3 of 5 based on 2328 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us