文件上载错误没有任何内容与给定的URI匹配

时间:2015-06-25 07:38:57

标签: python-2.7 http python-requests basehttpserver

我正在尝试使用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

我收到的错误消息显示“Nothing与给定的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>

有没有人有任何提示可能是什么问题?

0 个答案:

没有答案