user1687891
user1687891

Reputation: 864

How to call a php script in server from python

I am trying to run the php script which is in my server from the python project which is in my local machine.

I have tried following till now

Python Side:

# -*- coding: utf-8 -*-

import subprocess
import json
import sys
import os

def php(script_path):
    p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    return result

image_dimensions_json = str(php("http://XXX.XXX.XX.XX/logistic_admin/test1.php"))
dic = json.loads(image_dimensions_json)
print str(dic["0"]) + "|" + str(dic["1"])

php side:

test1.php

<?php
echo(json_encode(getimagesize($argv[1])));

?>

But I am facing following error:

Traceback (most recent call last):
  File "D:\folder\test.py", line 20, in <module>
    image_dimensions_json = str(php("http://XXX.XXX.XX.XX/logistic_admin/test1.php"))
  File "D:\folder\test.py", line 16, in php
    p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
  File "C:\Python27\Lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Python27\Lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Upvotes: 1

Views: 2156

Answers (2)

Skiller Dz
Skiller Dz

Reputation: 963

or you can use requests module:

import requests

def req(url):
    r = requests.get(url)
    return r.text

or urllib2 module:

import urllib2
def req(url):
   f = urllib2.urlopen(url)
   return f.read()

Upvotes: 0

Rakesh
Rakesh

Reputation: 82765

Try using urllib module.

Ex:

import urllib

def php(script_path):
    urlData = urllib.urlopen(script_path)
    return urlData.read()

Upvotes: 3

Related Questions