2017-08-14 377 views
1

在Web开发中,我是一个相当新的东西,有人可以请我指出帮助脚本在网页上运行.py脚本的正确方向。以下是我正在使用的那些。我必须创建一个html文件和一个php文件吗? 如果是这样,请帮助我。我有一台运行在Apache XAMPP上的内部服务器,并且配置为运行CGI,.py脚本。如何从网页运行python脚本?

工作流程:

上传>按钮来运行该脚本的.py下面>下载

上传脚本(PHP):

<?php 
if(isset($_POST['UploadButton'])){ //check if form was submitted 

$target_dir = '/opt/lampp/htdocs/pic-el/Dump/'; 
$target_file = $target_dir . basename($_FILES["filepath"]["name"]); 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
move_uploaded_file($_FILES["filepath"]["tmp_name"], $target_file); 
} 
?> 

Python脚本:

#!/usr/bin/env python 

import CGIHTTPServer 
CGIHTTPServer.test() 
import os 
import urllib 
import cgi 
import webcolors 
import xlsxwriter 
from PIL import Image 

filename = "/home/Desktop/tess/test1" 

imageNameArray = [] 


def downloadfile(urlData): 
    urllib.urlretrieve(urlData[0], urlData[1]) 
    print " image downloaded: " + str(urlData[1]) 
    return 


# open file to read 
with open("{0}.csv".format(filename), 'r') as csvfile: 
    # iterate on all lines 
    i = 0 
    for line in csvfile: 
     splitted_line = line.split(',') 
     # check if we have an image URL 
     if splitted_line[1] != '' and splitted_line[1] != "\n": 
      # urllib.urlretrieve(splitted_line[1], '/home/tf_files/images/{0}.jpg'.format (splitted_line[0])) 
      imageNameArray.append(
       (splitted_line[1], '/home/Desktop/tess/images/{0}.jpg'.format(splitted_line[0]))) 
      print "Image added to list for processing for {0}".format(splitted_line[0]) 
      i += 1 
     else: 
      print "No result for {0}".format(splitted_line[0]) 

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' 

from multiprocessing import Pool 

processPool = Pool(5) 
processPool.map(downloadfile, imageNameArray) 

# Create a workbook and add a worksheet. 
workbook = xlsxwriter.Workbook('output1.xlsx') 
worksheet = workbook.add_worksheet() 
# Start from the first cell. Rows and columns are zero indexed. 
row = 0 
col = 0 

# search for files in 'images' dir 
files_dir = os.getcwd() + '/images' 
files = os.listdir(files_dir) 


def closest_colour(requested_colour): 
    min_colours = {} 
    for key, name in webcolors.css3_hex_to_names.items(): 
     r_c, g_c, b_c = webcolors.hex_to_rgb(key) 
     rd = (r_c - requested_colour[0]) ** 2 
     gd = (g_c - requested_colour[1]) ** 2 
     bd = (b_c - requested_colour[2]) ** 2 
     min_colours[(rd + gd + bd)] = name 
    return min_colours[min(min_colours.keys())] 


def get_colour_name(requested_colour): 
    try: 
     closest_name = actual_name = webcolors.rgb_to_name(requested_colour) 
    except ValueError: 
     closest_name = closest_colour(requested_colour) 
     actual_name = None 
    return actual_name, closest_name 


for f in files: 
    if f.lower().endswith(('.png', '.jpg', '.jpeg')): 
     image_path = files_dir + '/' + f 
     im = Image.open(image_path) 
     n, cl = max(im.getcolors(im.size[0] * im.size[1])) 
     requested_colour = cl 
     actual_name, closest_name = get_colour_name(requested_colour) 


     width = im.size 
     if width < (500, 500): 
      worksheet.write(row, 4, "False") 
     else: 
      worksheet.write(row, 4, "True") 

     print image_path 
     print cl 
     print width 
     print "Actual colour name:", actual_name, ", closest colour name:", closest_name 


     worksheet.write_string(row, 1, image_path) 
     worksheet.write(row, 3, closest_name) 
     row += 1 





workbook.close() 
+0

https://stackoverflow.com/questions/9398560/how-do-i-run-a-python-script-on-my- web服务器 – MacBooc

+0

你也可以看一下https://www.djangoproject.com并使用Django作为你的网络服务器。 –

回答

0

你不能在网页上运行.py,只有你可以在服务器上运行,因为Python是一个服务器端编程。但是你可以从PHP运行python脚本(因为ü[R使用XAMPP。) 示例 -

<?php 
    $output = exec('./filename.py'); 
?> 
+0

当我尝试以上时似乎没有工作。如何在页面上显示错误? – Shri

+0

只是回声$输出。当你运行exec('./ filename.py')时,它会返回python的输出。 检查它。 http://php.net/manual/en/function.exec.php –

+0

注意:使用未定义的常量输出 - 在第3行的/opt/lampp/htdocs/IMG/ne.php中假定'输出' 输出 - 那是什么显示。无法理解这是什么意思 – Shri

0

你并不需要创建单独的PHP和HTML文件。

首先,当服务器返回的Apache2

sudo apt-get install libapache2-mod-wsgi

(它与WSGI连接的Apache2。)

其次,你需要创建一个配置文件,并

移动配置文件到Document_Root。

ex> server.conf 

WSGIScriptAlias /test /var/www/wsgi/main.py 

MainOption |要连接的地址| Python文件位置

三,重启apache2服务。

EXAMPLE_CODES

main.py

server.conf

+0

即时通讯,是在apache htdoc文件夹中的document_root文件夹? – Shri

+0

@Shri是在htdocs或/ var/www/html文件夹 –

+0

很酷......将chek恢复回来..谢谢 – Shri