1
브라우저는 python 스크립트를 실행하는 대신 코드를 보여줍니다.은 HTML에서 python 스크립트를 실행합니다
HTML 코드는 다음과 같습니다 :
<html><body>
<form enctype="multipart/form-data" action="/static/savefiles.py" method="post">
<p>File: <input type="file" name="file"></p>
<p><input type="submit" value="Upload"></p>
</form>
</body></html>
파이썬 코드는 다음과 같습니다 :
#!c:\Python27\python.exe
#!/usr/bin/python
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import cgi, os
import cgitb; cgitb.enable()
try: # Windows needs stdio set for binary mode.
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
pass
form = cgi.FieldStorage()
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid directory traversal attacks
fn = os.path.basename(fileitem.filename)
open(os.path.join('static/files/' + fn, 'wb')).write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print """\
Content-Type: text/html\n
<html><body>
<p>%s</p>
</body></html>
""" % (message,)
나는 포럼에 게시 다양한 솔루션을 시도했지만 그들 중 누구도에서 일하지 내 경우. 시정해야 할 부분에 대해 안내해주십시오. 빠른 도움을 주셔서 감사합니다.
어떤 웹 서버를 사용하고 있습니까? 난 당신이 cgi - bin으로 파이썬 스크립트로 폴더를 표시하도록 구성해야 할 것 같아요 – wubwubb
Google 애플 리케이션 엔진 에이 코드를 배포하고 있습니다. 필자는 Python을 처음 사용하고 비상 휴가중인 동료를 돕는 데있어 새로운 것을 알게되었습니다. 정확히 무엇을해야하는지 설명해 주시겠습니까? – user3556659