2017-11-22 4 views
1

複数のファイルをアップロードできるPython 3.6のTwisted Webserverを作成しようとしていますが、Pythonと物事の両方に新しいことがあります。私は理解できない問題に遭遇しました。複数のファイルアップロードに関する良い例は見つかりません。フォームが提出した後、私はそれらを保存することができますように、ファイルまたはファイルのホールドを取得する方法を理解していない私はツイストアップロードmuliple filesキーエラー

from twisted.web import server, resource 
from twisted.internet import reactor, endpoints 
import cgi 


class Counter(resource.Resource): 
    isLeaf = True 
    numberRequests = 0 

    def render_GET(self, request): 
     print("GET " + str(self.numberRequests)) 
     self.numberRequests += 1 
     # request.setHeader(b"content-type", b"text/plain") 
     # content = u"I am request #{}\n".format(self.numberRequests) 
     content = """<html> 
     <body> 
     <form enctype="multipart/form-data" method="POST"> 
      Text: <input name="text1" type="text" /><br /> 
      File: <input name="file1" type="file" multiple /><br /> 
      <input type="submit" /> 
     </form> 
     </body> 
     </html>""" 
     print(request.uri) 


     return content.encode("ascii") 

    def render_POST(selfself, request): 
     postheaders = request.getAllHeaders() 
     try: 
      postfile = cgi.FieldStorage(
       fp=request.content, 
       headers=postheaders, 
       environ={'REQUEST_METHOD': 'POST', 
         # 'CONTENT_TYPE': postheaders['content-type'], Gives builtins.KeyError: 'content-type' 
         } 
      ) 
     except Exception as e: 
      print('something went wrong: ' + str(e)) 

     filename = postfile["file"].filename #file1 tag also does not work 
     print(filename) 

     file = request.args["file"][0] #file1 tag also does not work 



endpoints.serverFromString(reactor, "tcp:1234").listen(server.Site(Counter())) 

reactor.run() 

エラーログ

C:\Users\theuser\AppData\Local\conda\conda\envs\py36\python.exe C:/Users/theuser/PycharmProjects/myproject/twweb.py 
GET 0 
b'/' 
# My comment POST starts here 
Unhandled Error 
Traceback (most recent call last): 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\site-packages\twisted\web\http.py", line 1614, in dataReceived 
    finishCallback(data[contentLength:]) 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\site-packages\twisted\web\http.py", line 2029, in _finishRequestBody 
    self.allContentReceived() 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\site-packages\twisted\web\http.py", line 2104, in allContentReceived 
    req.requestReceived(command, path, version) 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\site-packages\twisted\web\http.py", line 866, in requestReceived 
    self.process() 
--- <exception caught here> --- 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\site-packages\twisted\web\server.py", line 195, in process 
    self.render(resrc) 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\site-packages\twisted\web\server.py", line 255, in render 
    body = resrc.render(self) 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\site-packages\twisted\web\resource.py", line 250, in render 
    return m(request) 
    File "C:/Users/theuser/PycharmProjects/myproject/twweb.py", line 42, in render_POST 
    filename = postfile["file"].filename #file1 tag also does not work 
    File "C:\Users\theuser\AppData\Local\conda\conda\envs\py36\lib\cgi.py", line 604, in __getitem__ 
    raise KeyError(key) 
builtins.KeyError: 'file' 

を取得するには、次のコードで

アップロードrender_POST内にあります。この投稿SOにこの問題はないようです。最終的なアプリは、複数のユーザーのためにこれを非同期で行うことができるはずですが、それ以前には簡単なアプリだけを稼働させることができてうれしいです。 3+ PythonでのPython 3.6

答えて

1

FieldStorageでWindows 10上でcondaを使用して

はバイトではなく文字列としてキーを持つ辞書を返します。キーはb""が付加されていることを

postfile[ b"file" ] 

がお知らせ:だから、このようなキーにアクセスする必要があります。あなたがPythonを初めて習得していて、Python 3と2の文字列の間の変更を認識していないのであれば、ちょっと混乱します。

また、私はsimilar questionに返答しましたが、Python 3.4では正常に動作することができませんでしたが、正確には動作しなかったことを覚えていません。うまくいけば、あなたは3.6を使用して問題に遭遇しないでください。

+0

b'content-type 'で、私はcgi.FieldStorageに問題を渡します。しかし、私は後でファイルのためのフィールドを取得することはできません、それは2つのキーの名前とファイル名をそれらのフィールドを持っている解析するが、私はそれを得ることはできません。私はPython 2.7に切り替えて、これを動作させることができるので、3.4からの問題はまだ3.6に存在すると思います – bits