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