Sending data from Moodle php to python CGI and get it back

Sending data from Moodle php to python CGI and get it back

de Станислав Швец -
Number of replies: 2

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

I made var_dump($content); 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?

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.

Sorry for bad English.

Promeyo de puntuacions: -
In reply to Станислав Швец

Re: Sending data from Moodle php to python CGI and get it back

de Dominique Palumbo -
Imachen de Particularly helpful Moodlers Imachen de Plugin developers
Hi,
I'm not sure I've totally understand everythings.
But maybe you've to replace that line

$morgot_data = file_get_contents($morgot_link);

By something like that

$url = $morgot_link;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_PORT , 80);
curl_setopt ($ch, CURLOPT_TIMEOUT , 300);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
In reply to Dominique Palumbo

Re: Sending data from Moodle php to python CGI and get it back

de Станислав Швец -
Greetings! Thanks for trying to help me! What project moment needs to be explained?
I tried your code, but unfourtunatly, it did not work. Same empty output. Maybe It is need to place something inside $ch = curl_init(); ?
Like http://localhost/diplom/local/tokenize/morgot.py?someamountoftext= ?
I am, just, not very familliar with curl theme.
Meanwhile I cheked line allow_url_fopen in php.ini of XAMPP. It was on. So it is not server side problem.

P.S. Sorry for late responses. I am constantly busy.