0

Python 2.7 リクエストと BaseHTTPServer を使用してファイルをアップロードしようとしています。サーバーの POST 構成は次のとおりです。

    try:

        ctype, pdict = cgi.parse_header(s.headers.getheader('content-type'))
        if ctype == 'multipart/form-data' :
            fs = cgi.FieldStorage( fp = s.rfile, 
                                    headers = s.headers, 
                                    environ={ 'REQUEST_METHOD':'POST' }    
                                  )
        else: raise Exception("Unexpected POST request")
        fs_up = fs['upfile']
        filename = os.path.split(fs_up.filename)[1] # strip the path, if it presents
        CWD = os.path.abspath('.')
        fullname = os.path.join(CWD, filename)

        with open(fullname, 'wb') as o:
            o.write( fs_up.file.read() )
            s.send_response(200)
            s.end_headers()


    except Exception as e:

        print e
        s.send_error(404,'POST to "%s" failed: %s' % (s.path, str(e)) )

クライアントは非常に単純です

url = 'http://<Server IP>'
files = {'file': open('C:/Users/hkhrais/Desktop/bigtasty.txt', 'rb')}

r = requests.post(url, files=files)
print r.status_code
print r.text

「指定された URI に一致するものはありません」というエラー メッセージが表示されます。

<head>
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code 404.
<p>Message: POST to "/" failed: 'upfile'.
<p>Error code explanation: 404 = Nothing matches the given URI.
</body>

問題が何であるかについてのヒントはありますか?

4

0 に答える 0