Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80542

Re: Communicating with a PHP script (and pretending I'm a browser)

References (1 earlier) <CA+DCN_u=dQpXXeNvKKvMR0HYE1=RQEs8oaxf8z7F+_Y5+x=c1g@mail.gmail.com> <CACwCsY4ukfA0F8BsW_seVRW01dqBfxs-oZ5s1jidQMTQoUgXAw@mail.gmail.com> <CA+DCN_tyd17jbvYQXtF-JR=bB9EPUX8LqqVe0rv612qzh_T_kA@mail.gmail.com> <CACwCsY6Opf-BhWicaNboWJKwDCSJJC889s2BtbDmQhUrHph3Hw@mail.gmail.com> <CA+DCN_uF8mATeZ168_hvHPYQO8yYEoB6ERFga_vkt256OJ9=dg@mail.gmail.com>
Date 2014-11-11 12:33 -0500
Subject Re: Communicating with a PHP script (and pretending I'm a browser)
From Larry Martell <larry.martell@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.15680.1415727225.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Nov 11, 2014 at 12:31 PM, Marc Aymerich <glicerinu@gmail.com> wrote:
> On Tue, Nov 11, 2014 at 6:26 PM, Larry Martell <larry.martell@gmail.com> wrote:
>> On Tue, Nov 11, 2014 at 12:18 PM, Marc Aymerich <glicerinu@gmail.com> wrote:
>>> On Tue, Nov 11, 2014 at 5:43 PM, Larry Martell <larry.martell@gmail.com> wrote:
>>>> On Tue, Nov 11, 2014 at 11:00 AM, Marc Aymerich <glicerinu@gmail.com> wrote:
>>>>> On Tue, Nov 11, 2014 at 4:48 PM, Larry Martell <larry.martell@gmail.com> wrote:
>>>>>>
>>>>>> I have a PHP app that I want to convert to django. But I want to do it
>>>>>> stages. All the heavy lifting is in the PHP code, so first, I want to
>>>>>> just use templates and views to generate the HTML, but still call the
>>>>>> PHP code. Then later convert the PHP to python.
>>>>>>
>>>>>> My issue is that the PHP code expects to get all it's input from the
>>>>>> REQUEST object and I've consumed that in the view. Is there any way I
>>>>>> can somehow supply that to the PHP code?
>>>>>>
>>>>>> Is there some way python can communicate like curl ... it needs to
>>>>>> send the request string in the body of a POST request to the URL that
>>>>>> will route to the PHP script and get the output back.
>>>>>
>>>>>
>>>>> Yes,
>>>>> I supose you can extract the needed information from the django
>>>>> Request object and call the php script passing the needed variables as
>>>>> environment state.
>>>>>
>>>>> as a guideline you can do something like
>>>>>
>>>>> cmd = (
>>>>>     'REDIRECT_STATUS=200 '
>>>>>     'REQUEST_METHOD=GET '
>>>>>     'SCRIPT_FILENAME=htdocs/index.php '
>>>>>     'SCRIPT_NAME=/index.php '
>>>>>     'PATH_INFO=/ '
>>>>>     'SERVER_NAME=site.tld '
>>>>>     'SERVER_PROTOCOL=HTTP/1.1 '
>>>>>     'REQUEST_URI=/nl/page '
>>>>>     'HTTP_HOST=site.tld '
>>>>>     '/usr/bin/php-cgi'
>>>>> )
>>>>> subprocess.Popen(cmd, stdout=subprocess.PIPE)
>>>>
>>>> Thanks very much Marc. In the example, how is the request string passed in?
>>>
>>>
>>> Yeah, a more complete example would be
>>>
>>> cmd = (
>>>     'REDIRECT_STATUS={status} '
>>>     'REQUEST_METHOD={method} '
>>>     'SCRIPT_FILENAME={file} '
>>>     'SCRIPT_NAME=/index.php '
>>>     'PATH_INFO={path} '
>>>     'SERVER_NAME=site.tld '
>>>     'SERVER_PROTOCOL=HTTP/1.1 '
>>>     'REQUEST_URI={path} '
>>>     'HTTP_HOST=site.tld '
>>>     '/usr/bin/php-cgi'
>>> ).format(
>>>     status=request.status,
>>>     method=request.method,
>>>     path=request.path,
>>>     file=my_php_path_mapper(request.path),
>>> )
>>>
>>> php = subprocess.Popen(cmd, stdout=subprocess.PIPE)
>>> response, err = php.communicate()
>>> if php.return_code != 0:
>>>     return ResponseError(content=err)
>>> return Response(content=response)
>>>
>>>
>>> still incomplete and mostly wrong, but good enough to illustrate the
>>> main pattern :)
>>
>>
>> So I would put the contents of what I want in the request object in
>> the file request.path?
>
> I just invented the attribute names that django actually uses, but you
> can look at the excellent django documentation for the correct ones
>
> https://docs.djangoproject.com/en/dev/ref/request-response/
>
> still this is a toy example and probably it would be a pain in the ass
> to really map the mixed URLs between the two web applications.


OK, I see what you're saying now. Thanks.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Communicating with a PHP script (and pretending I'm a browser) Larry Martell <larry.martell@gmail.com> - 2014-11-11 12:33 -0500

csiph-web