0

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 ?

client eastwood
  • 103
  • 2
  • 8
  • Can you access the server at the url you're specifying in the client? Or try to upload with a command line tool (e.g. curl or wget). –  Jun 25 '15 at 08:23
  • If you must catch the exception, try using the traceback module to get the full traceback. But for development, just re-raise the exception in the server (because 'upfile' isn't much of an exception message). –  Jun 25 '15 at 08:26
  • Is it correct that you have 'file' as the key in your dict that is sent, but you use 'upfile' on the receiving side? –  Jun 25 '15 at 08:28
  • @Evert , i can't believe i made that mistake, it was 'file' as the key and 'upfile' on the receiving side, please comment that so i can accept it as answer :) – client eastwood Jun 25 '15 at 09:02

0 Answers0