我正在尝试通过 sub-process module 将 key:value 对发送到 perl 脚本。我只是尝试了一个简单的例子,它工作正常。如何将键:值对传递给 perl 脚本.....?
Ans还如何将多个输入字段值发送到perls脚本>下面是我的代码
视图.py
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from subprocess import Popen, PIPE, sys
import subprocess
def ex(request):
myName = 'Pervez'
if 'firstname' in request.POST:
myName = (request.POST['firstname'])
p = Popen(['/home/pervez/Desktop/simplepage/simplepage/templates/ex.pl', myName],stdin=PIPE, stdout=PIPE)
p.stdin.close()
result = p.stdout.read()
return render_to_response('ex.html', {'name': result},context_instance=RequestContext(request))
ex.html
{{ name }}
<FORM METHOD="POST" ACTION="" NAME="SIGNUPFORM">
{% csrf_token %}
<fieldset>
<legend>Signup Form</legend>
<table>
<tr><td>First Name</td><td><INPUT SIZE=20 NAME="firstname"> </td></tr>
<tr><td>Last Name</td><td><INPUT SIZE=20 NAME="lastname"></td></tr>
<tr><td>User Name</td><td><INPUT SIZE=20 NAME="username"></td></tr>
<tr><td>Password</td><td><INPUT SIZE=20 NAME="password"></td></tr>
<tr><td>Verify Password</td><td><INPUT SIZE=20 NAME="vpass"></td></tr>
<tr><td>Your E-mail</td><td><INPUT SIZE=20 NAME="email_id"></td></tr>
<tr><td align="right"> </td>
<td><input type="submit" value="Submit">
<input type="Reset" value="Clear Form">
</td>
</tr>
</table>
</fieldset>
</form>
例
#!/usr/bin/perl
$|=1; # Flush immediately.
print "Content-Type: text/plain\n\n";
read STDIN, $form, $ENV{'CONTENT_LENGTH'};
foreach $pair (split('&', $form)) {
if ($pair =~ /(.*)=(.*)/) { # found key=value;
($key, $value) = ($1, $2); # get key, value.
$value =~ s/\+/ /g; # substitute spaces for + signs.
$value =~ s/%(..)/pack('c',hex($1))/eg;
$inputs{$key} = $value; # Create Associative Array.
}
}
foreach $item (keys(%inputs)) {
print "$item - $inputs{$item}\n";
}