2017-08-14 197 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