1

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
5
  • You probably need to use the urllib or requests module Commented Jun 21, 2018 at 9:40
  • @Rakesh how to use it. Can you give me an example. Commented Jun 21, 2018 at 9:41
  • Why are you calling your PHP over an HTTP web server? Are both PHP and Python on the same server or not? Commented Jun 21, 2018 at 9:42
  • @delboy1978uk no my php code is in server and python is in my desktop Commented Jun 21, 2018 at 9:43
  • ah ok fair enough then Commented Jun 21, 2018 at 9:44

2 Answers 2

3

Try using urllib module.

Ex:

import urllib

def php(script_path):
    urlData = urllib.urlopen(script_path)
    return urlData.read()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Rakesh
0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.