我找到了一个很好的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
例外。
答案 0 :(得分:3)
这是一个基本的Python问题。您需要先导入模块才能使用它。所以只需在脚本顶部执行import urllib
即可。