【发布时间】:2011-04-25 03:27:33
【问题描述】:
我找到了一个不错的 python 模块,用于通过名为poster 的 HTTP POST 向远程服务器发送数据。所以我在我的 django 应用程序上写了一个简单的视图来接收和存储数据,然后尝试发送一些文件。不幸的是,即使我已经按照指令中的说明设置了所有内容,但我得到了Internal Server Error。谁能看到我做错了什么?
这是视图函数:
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 异常。
【问题讨论】:
-
问题似乎不在于视图功能,而在于您发送请求的代码。
-
在使用了不同的方法(使用默认的 httplib 以及此处显示的 urllib2)之后,我设法收到了它一次,但现在我不知道它是如何以及为什么起作用的:/
标签: python django http file-upload