2010-10-13 89 views
0

我发现了一个很好的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例外。

+0

这个问题似乎并没有与视图功能,其代码与您发送请求。 – 2010-10-14 09:49:10

+0

玩过不同的方法后(使用默认的httplib以及在这里显示的urllib2)我设法收到它一次,但现在我不知道如何以及为什么它的工作:/ – mizou 2010-10-14 13:06:41

回答

3

这是一个基本的Python问题。您需要先导入一个模块,然后才能使用它。所以只需在脚本的顶部执行import urllib,它应该可以工作。

+0

他没有在他的视图功能中使用urllib。 – 2010-10-14 09:46:39

+0

不,错误在于他在控制台中使用的代码来测试函数。 – 2010-10-14 10:02:11

+0

这只是我在下一行更正的拼写错误。编辑 – mizou 2010-10-14 13:05:12

相关问题