Lighttpd+Pythonå®ç°å¨çº¿æ°æ®å¤ç
å åæ§½ä¸ä¸Apacheçé ç½®æä»¶,å¥½é¿æ¶é´æ²¡æä¹é ç½®è¿,åç°åç§çæ¬ä¹é´çè¯æ³ä¹±ä¸å «ç³ç,æå¾å»ç ç©¶äº,è¿æ¯ç¨ç®åç²æ´ææçLighttpdå§,æ§è½åºæ¬ä¸ç¨èè.
ä¹åç¨python+pandasåäºä¸ªæ°æ®å¤ççèæ¬,æ³æ¾å°æå¡å¨ä¸è¿ç¨è°ç¨,B/S模å¼å°±å¯ä»¥ççè£ ç¯å¢äº.è¿æ¯å¤´ä¸æ¬¡åè¿ç§å®ç°,å ¶å®lighttpdå°±æä¾ä¸ªåºæ¬çhttpæå¡,å ·ä½çæ°æ®æä½éè¿cgiæç»pythonèæ¬æ¥å¤ç,å®ç°çè¿ç¨ä¸ä¹è¸©äºä¸å°å,æè¿ç¨æ´çè®°å½ä¸ä¸å§:
1.å®è£ é ç½®lighttpd
CentOS7ä¸é¢æè¿ä¸ªè¿æ¯å¾ç®åç
# yum install -y lighttpd
# systemctl enable lighttpd
# systemctl start lighttpd
ç«ç¹æä»¶ææ¾å¨äº/wwwä¸é¢,æä»¥éè¦ä¿®æ¹ä¸ä¸lighttpdçé ç½®
# vim /etc/lighttpd/lighttpd.conf
ä¿®æ¹é¨å:
var.server_root = "/www"
server.document-root = server_root
server.use-ipv6 = "disable"
server.max-fds = 2048
é ç½®å¯ç¨CGI,å ¶ä¸pythonçèæ¬æ¾å¨/www/cgi-binä¸é¢.è¿ééç¹æ¯åé¢å¢å ç$HTTPé£ä¸å
# vim /etc/lighttpd/modules.conf
ä¿®æ¹å
容:
server.modules = (
"mod_access",
"mod_cgi",
)
$HTTP["url"] =~ "^/cgi-bin/" {
cgi.assign = ( ".py" => "/usr/bin/python" )
é ç½®æ¹å¥½å,éå¯ä¸ä¸lighttpdæå¡
# systemctl restart lighttpd
妿æå¡å¯å¨ä¸æå,å¯ä»¥ä½¿ç¨æ§å¶å°ç´æ¥è¾åºçæ¹å¼è¿è¡lighttpd
[[email protected] lighttpd]# lighttpd -f /etc/lighttpd/lighttpd.conf -D
2018-10-28 12:19:32: (server.c.1346) can't have more connections than fds/2: 1024 1024
åºç°è¿ç§é误æ¯ç±äºlighttpd.conféé¢server.max-fds = 2048è¿ä¸è¡æ²¡æçæ,æ£æ¥ä¸ä¸é ç½®æä»¶å 容.
é ç½®é²ç«å¢å¼æ¾80端å£
# firewall-cmd --add-service=http --permanent
2.éæé¡µé¢ä»£ç /www/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>æ°æ®å¤ç</title>
</head>
<body>
<h1>请ä¸ä¼ è¦å¤ççæä»¶:</h1>
<form enctype="multipart/form-data" action="/cgi-bin/my.py" method="POST">
<input type="file" name="filename" />
<input type="submit" value="确认ä¸ä¼ " />
</form>
</body>
</html>
3.pythonèæ¬ä»£ç /www/cgi-bin/my.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
import cgi
import os
import cgitb
cgitb.enable()
import pandas as pd
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# print(fileitem.filename)
# Test if the file was uploaded
err = 'No error'
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
try:
df = pd.read_excel('/tmp/' + fn)
gp = df.groupby('å¿åº').count()
gp.to_excel('/www/xls/new.xlsx')
except Exception as e:
#err = e + '\n fn: ' + fn
err = e
#message = message + df.head(3)
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>æ°æ®å¤ç</title>
</head>
<body>
<h1>%s</h1>
<hr>
<h1>%s</h1>
<a href='/xls/new.xlsx'><h1>ä¸è½½Download</h1></a>
</body>
</html>
""" % (message, err)
è¿ééè¦è¡¥å 说æä¸ä¸,ç³»ç»é»è®¤å¸¦çPython 2.7坹䏿å¾ä¸å好,ä¸é¢ç代ç è¿è¡å¯è½ä¼åºé®é¢,è¿ä¸ªåéååç¨Python3.xå¡«å§
2018.10.28æ´æ°
åºæ¬ä¸çäºå ¨é¨éæäºä¸é,使ç¨Bootstrap3åçå端,使ç¨ajax弿¥æäº¤è¡¨å让åå°çpythonèæ¬æ§è¡,æææè§è¿æ¯æ£æ£åç.biaä¸ä¸ææå¾å代ç
/www/index.html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>æ°æ®å¤çå¹³å°</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<a class="navbar-brand" href="#">CATIçµè¯è°æ¥ç³»ç»æ°æ®å¤çå¹³å°</a>
<ul class="nav navbar-nav">
<li class="active">
<a href="#">é¦é¡µ</a>
</li>
<li>
<a id="aboutBtn" href="#">å
³äº</a>
</li>
</ul>
</nav>
<div class="container" style="padding-top:60px">
<div class="jumbotron">
<form id="mainform" enctype="multipart/form-data">
<div class="container">
<h2>å¹³å®å»ºè®¾å
¬ä¼å®å
¨æåæ¿æ³æºå
³æ§æ³æ»¡æåº¦è°æ¥</h2>
<p>é®å·è¿åº¦æ°æ®åå¿åºæ±æ»</p>
<p>
<div class="form-group">
<label for="filename">æå¼xlsæä»¶</label>
<input type="file" id="filename" name="filename">
<p class="help-block">è¯·éæ©ä»CATIå¹³å°å¯¼åºçé®å·æ°æ®xlsæä»¶</p>
</div>
</p>
<button type="submit" class="btn btn-primary btn-lg">å¼å§å¤ç</button>
<a id="download" href="/xls/new.xlsx" class="btn btn-default btn-lg" style="display:none">ç¹å»ä¸è½½</a>
<div id="progressbar" style="margin-top:1em;display:none" class="progress progress-striped active">
<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="60" aria-valuemin="0"
aria-valuemax="100" style="width: 100%;">æ£å¨å¤ç...
</div>
</div>
<div id="alert" class="alert alert-success alert-dismissible fade in" style="display:none;margin-top:1em">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>æ±æ»å®æ</strong>请ç¹å»ä¸è½½
</div>
</div>
</form>
</div>
</div>
<div class="modal fade" id="about" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">å
³äºæ¬ç«ç¹</h4>
</div>
<div class="modal-body">
<p>åºäºLighttpd/Python CGI/Pandaså¼å</p>
<p>è¿é¹ä¼ 2018.10.28</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">å
³é</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<script>
$("#aboutBtn").click(function () {
$("#about").modal("toggle");
});
$("#mainform").submit(function (event) {
event.preventDefault();
$("#progressbar").show();
var formData = new FormData();
formData.append('filename', $("#filename")[0].files[0]);
$.ajax({
async: true,
type: "POST",
url: "/cgi-bin/my.py",
data: formData,
processData: false,
contentType: false,
dataType: "json",
success: function (data) {
$("#progressbar").hide();
$("#alert").show();
$("#download").show();
},
error: function (data) {
alert('error:' + JSON.stringify(data))
}
})
})
</script>
</body>
</html>
/www/cgi-bin/my.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
import pandas as pd
import cgi
import os
import cgitb
import json
cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# print(fileitem.filename)
# Test if the file was uploaded
err = 'Success!'
result = {}
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
try:
df = pd.read_excel('/tmp/' + fn)
df.to_excel('/www/xls/new.xlsx')
except Exception as e:
err = e
result['success'] = True
message = 'The file "' + fn + '" was uploaded successfully'
else:
result['success'] = False
message = 'No file was uploaded'
#result = {'success':'true','msg': message}
#result['success'] = True
result['msg'] = message
print("Content-Type: application/json\n\n")
print(json.dumps(result))