views:

50

answers:

1

I've found a nice python module for sending data to remote servers via HTTP POST called poster. So I've wrote a simple view on my django app to receive and store data and then tried to send some file. Unfortunatelly even though I've set everything as it was shown in the instruction I'm getting Internal Server Error. Can anyone maybe see what am I doing wrong ?

Here's the view function :

def upload(request):
    for key, file in request.FILES.items():
        path = '/site_media/remote/'+ file.name
        dest = open(path.encode('utf-8'), 'wb+')
        if file.multiple_chunks:
            for c in file.chunks():
                dest.write(c)
        else:
            dest.write(file.read())
        dest.close()

Here's the module instruction : http://atlee.ca/software/poster/ and some instruction I was basing on http://www.laurentluce.com/?p=20

Here's the traceback:

In [15]: print urllib2.urlopen(request).read()
-------> print(urllib2.urlopen(request).read())
---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)

/home/rails/ntt/<ipython console> in <module>()

/bin/python-2.6.2/lib/python2.6/urllib2.pyc in urlopen(url, data, timeout)
    122     if _opener is None:
    123         _opener = build_opener()
--> 124     return _opener.open(url, data, timeout)
    125
    126 def install_opener(opener):

/bin/python-2.6.2/lib/python2.6/urllib2.pyc in open(self, fullurl, data, timeout)
    387         for processor in self.process_response.get(protocol, []):
    388             meth = getattr(processor, meth_name)
--> 389             response = meth(req, response)
    390
    391         return response

/bin/python-2.6.2/lib/python2.6/urllib2.pyc in http_response(self, request, response)
    500         if not (200 <= code < 300):
    501             response = self.parent.error(
--> 502                 'http', request, response, code, msg, hdrs)
    503
    504         return response

/bin/python-2.6.2/lib/python2.6/urllib2.pyc in error(self, proto, *args)
    425         if http_err:
    426             args = (dict, 'default', 'http_error_default') + orig_args
--> 427             return self._call_chain(*args)
    428
    429 # XXX probably also want an abstract factory that knows when it makes


/bin/python-2.6.2/lib/python2.6/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
    359             func = getattr(handler, meth_name)
    360
--> 361             result = func(*args)
    362             if result is not None:
    363                 return result

/bin/python-2.6.2/lib/python2.6/urllib2.pyc in http_error_default(self, req, fp, code, msg, hdrs)
    508 class HTTPDefaultErrorHandler(BaseHandler):
    509     def http_error_default(self, req, fp, code, msg, hdrs):
--> 510         raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    511
    512 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 500: Internal Server Error

I get the same error when I'm trying to send my file to a php function (from www.w3schools.com/php/php_file_upload.asp
Also I've checked with wireshark and my POST request is sent without any problems but then something bad happens and I'm getting this 500. May it be that the server is somehow limited ? Uploading files in applications running on it works fluently.


Placing this view funtion with url on different server, and running :

import httplib
conn = httplib.HTTPConnection("address")
f = open("file, "rb")
conn.request("POST","/upload", f, headers)
response = conn.getresponse()

Raised : BadStatusLine exception.

+1  A: 

This is a basic Python question. You need to import a module before you can use it. So just do import urllib at the top of the script and it should work.

Daniel Roseman
He has not used urllib in his view function.
anand
No, the error is in the code he's using in the console to test the function.
Daniel Roseman
it's just a misspell I've corrected in the next line. edited
mizou