我發現了一個很好的python模塊,用於通過HTTP POST將數據發送到遠程服務器,名爲poster。所以我在我的django應用程序上寫了一個簡單的視圖來接收和存儲數據,然後嘗試發送一些文件。不幸的是,即使我已經設置了一切,因爲它顯示在指令中,我得到Internal Server Error
。任何人都可以看到我做錯了什麼?500將文件從python發送到django時發生錯誤
這裏的視圖功能:
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()
這裏的模塊指令:http://atlee.ca/software/poster/和一些指令我是立足於http://www.laurentluce.com/?p=20
這裏的回溯:
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
我當我遇到同樣的錯誤試圖發送我的文件到一個PHP函數(從www.w3schools.com/php/php_file_upload.asp
此外我已與wireshark檢查和我的POST請求發送沒有任何問題,但然後發生了一些不好的事情,得到這個500.可能是服務器在某種程度上有限嗎?在運行的應用程序中上傳文件的過程非常流暢。
配售這一觀點與不同服務器上的URL功能,並運行:
import httplib
conn = httplib.HTTPConnection("address")
f = open("file, "rb")
conn.request("POST","/upload", f, headers)
response = conn.getresponse()
募集:BadStatusLine
例外。
這個問題似乎並沒有與視圖功能,其代碼與您發送請求。 – 2010-10-14 09:49:10
玩過不同的方法後(使用默認的httplib以及在這裏顯示的urllib2)我設法收到它一次,但現在我不知道如何以及爲什麼它的工作:/ – mizou 2010-10-14 13:06:41