File upload error Nothing matches the given URI

2.2k views Asked by At

i'm trying to upload a file using python 2.7 requests and BaseHTTPServer, here's the server POST config

    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)) )

the client is quite simple

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

The error message which i get says "Nothing matches the given 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>

Does anyone has any tips what the problem could be ?

0

There are 0 answers