passing value from Python3 script back to RewriteMap MapType prg in Apache24

Benedikt Kroll benedikt.kroll at der-arbeitende.de
Tue Nov 13 16:05:25 EST 2018


Thank you for your responses!

The solution I just found is the following: Even though the python 
script has the necessary execute permissions, the RewriteMap definition 
required that the python interpreter is included in the Apache config file:

RewriteMap extrw "prg:/usr/bin/python3 /opt/extrw.py"

Now it worked using this script:

while True:
      arg = sys.stdin.readline() # makes sense, but will work without
      newValue = "/index.html" # placeholder for testing
      sys.stdout.write(newValue + "\n")
      sys.stdout.flush()



Am 12.11.18 um 23:57 schrieb MRAB:
> On 2018-11-12 09:39, Benedikt Kroll wrote:
>> Hi,
>>
>> using a Python script as a Rewrite Map with MapType prg in Apache 2.4, 
>> I'm having trouble
>> passing the value back to Apache.
>>
>> The log says "map lookup OK", but the value is empty.
>>
>> According to the Apache documentation 
>> (https://httpd.apache.org/docs/current/rewrite/rewritemap.html#prg), 
>> the script "should return one new-line terminated response string on 
>> STDOUT" which is then used as the rewrite target.
>>
>> I have tried to do this using:
>> sys.stdout.write(newValue + "/n")
>> sys.stdout.flush()
>>
>> However, Apache receives only an empty return value (with no error).
>>
>> On Stackoverflow and others, I found and tried out some examples, which
>> in some cases where quite old, so probably written for older versions.
>>
>> The following is what I extracted from the more current hints I could 
>> find.
>>
>>
>> *.conf
>> RewriteEngine On
>> RewriteMap extrw "prg:/opt/extrw.py"
>> RewriteRule "^(.*)" "${extrw:%{REQUEST_URI}}"
>>
>>
>> /opt/extrw.py
>> import sys
>> while True:
>>     newValue = "/index.html" # placeholder for testing
>>     sys.stdout.write(newValue + "/n")
>>     sys.stdout.flush()
>>
>>
>> log for curl 127.0.0.1/abc
>> init rewrite engine with requested uri /abc
>> applying pattern '^(.*)' to uri '/abc'
>> map lookup OK: map=extrw key=/abc -> val=
>> rewrite '/abc' -> ''
>> local path result:
>>
>>
>> The expected result would be to have "map lookup OK: map=extrw key=/abc
>> -> val=/index.html" in the third line.
>>
>> Any help would be appreciated!
>>
> The examples I've found are also reading from stdin, so I'd suggest 
> trying that:
> 
> import sys
> 
> while True:
>      arg = sys.stdin.readline()
>      newValue = "/index.html" # placeholder for testing
>      sys.stdout.write(newValue + "/n")
>      sys.stdout.flush()



More information about the Python-list mailing list