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)

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <larry.martell@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'django.': 0.05; 'output': 0.05; 'attribute': 0.07; 'variables': 0.07; 'string': 0.09; 'back.': 0.09; 'mixed': 0.09; 'subject:script': 0.09; 'wrong,': 0.09; 'yeah,': 0.09; 'python': 0.11; 'django': 0.11; 'mostly': 0.14; 'ass': 0.16; 'cmd': 0.16; 'code?': 0.16; 'expects': 0.16; 'illustrate': 0.16; 'incomplete': 0.16; 'subject:PHP': 0.16; 'url:djangoproject': 0.16; 'uses,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'app': 0.19; 'passing': 0.19; "skip:' 30": 0.19; 'skip:f 30': 0.19; 'later': 0.20; 'thanks.': 0.20; '>>>': 0.22; 'code,': 0.22; 'input': 0.22; 'example': 0.22; 'saying': 0.22; 'to:name:python-list@python.org': 0.22; 'url:dev': 0.24; 'environment': 0.24; "i've": 0.25; 'script': 0.25; 'first,': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'extract': 0.31; 'larry': 0.31; 'marc': 0.31; 'view.': 0.31; 'file': 0.32; 'probably': 0.32; 'subject: (': 0.35; 'subject:with': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'html,': 0.36; 'templates': 0.36; 'url:ref': 0.36; 'thanks': 0.36; 'example,': 0.37; 'two': 0.37; 'nov': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'how': 0.40; 'subject:skip:C 10': 0.61; "you're": 0.61; 'complete': 0.62; 'information': 0.63; 'map': 0.64; 'more': 0.64; 'between': 0.67; 'heavy': 0.81; 'curl': 0.84; 'guideline': 0.84; 'lifting': 0.84; 'pain': 0.84; 'route': 0.84; 'toy': 0.84; 'response,': 0.91; 'state.': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Bj86hqbQNSdWwrD2QRvKJqk6EARGNi/yrnG5AJxaxsQ=; b=WmCPxCFrM1KMkxO0FKDoVK0Sfs/Bq+A6VDV4sO+xMplvOhBRmUz2a5ciqU9H0Gh5YJ r2DSboL+qCiqGxgo7HtBeXfB3MwrUzrecNPAYZRU8VciKSukY1kt47L1JTvMdqmQxibj N04W/c2YCGAZojBA71XdMFCmp9e9Xj6IAaOZK7XjnZTY7qRV54qQ6J03idTEAG5QM65P So/dmzY8kR+6bXKlbBm7G6xiILlci0hxB54E4wjbP9LM0FNJdXaCVR5sgN2i0Oa557YS hecT9Gz7if0Vwp0IF4Ds/lbPOUv4cbJQbzF5p/1TzSJlf5jF0X4PFTACvmhSqOGiy/As SSng==
MIME-Version 1.0
X-Received by 10.180.75.38 with SMTP id z6mr42161390wiv.81.1415727223687; Tue, 11 Nov 2014 09:33:43 -0800 (PST)
In-Reply-To <CA+DCN_uF8mATeZ168_hvHPYQO8yYEoB6ERFga_vkt256OJ9=dg@mail.gmail.com>
References <CACwCsY46yYFQwdYdriTSYSvLYF=xs6L2zanN9gofkWkoHvj_fA@mail.gmail.com> <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 Tue, 11 Nov 2014 12:33:43 -0500
Subject Re: Communicating with a PHP script (and pretending I'm a browser)
From Larry Martell <larry.martell@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15680.1415727225.18130.python-list@python.org> (permalink)
Lines 89
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415727225 news.xs4all.nl 2907 [2001:888:2000:d::a6]:51944
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 7849
X-Received-Body-CRC 2763648995
Xref csiph.com comp.lang.python:80542

Show key headers only | 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