2014-04-21 2 views
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,) 

나는 포럼에 게시 다양한 솔루션을 시도했지만 그들 중 누구도에서 일하지 내 경우. 시정해야 할 부분에 대해 안내해주십시오. 빠른 도움을 주셔서 감사합니다.

+0

어떤 웹 서버를 사용하고 있습니까? 난 당신이 cgi - bin으로 파이썬 스크립트로 폴더를 표시하도록 구성해야 할 것 같아요 – wubwubb

+0

Google 애플 리케이션 엔진 에이 코드를 배포하고 있습니다. 필자는 Python을 처음 사용하고 비상 휴가중인 동료를 돕는 데있어 새로운 것을 알게되었습니다. 정확히 무엇을해야하는지 설명해 주시겠습니까? – user3556659

답변

0

나는이 질문에 비틀 거리며 똑같은 문제가있다. 지금 이것에 대한 해답이 있습니까?

코드가 작동하지 않는 이유는 HTML이 파이썬 파일로 해석하지 않기 때문입니다. 이것은 내가 아직 내 문제를 해결할 수 없기 때문에 지금까지 이해 한만큼입니다. 파이썬 파일을 이해하기 위해서는 html 용 통역사가 필요합니다.

관련 문제