Python Urllib2-Django-管道断裂

Python Urllib2-Django-管道断裂,python,django,urllib2,Python,Django,Urllib2,我是Django的新手,在将数据发布到Django Web应用程序时遇到一些问题 在django方面,我有一个表单(由couchdb-couchdbkit-django-ext支持)。 在客户端,我使用urllib2批量发布数据(通过数据列表逐个发布) 在一条记录(一次post传递)之后,它得到下面的消息,再次运行下一行传递,我再次得到此消息: res = urllib2.urlopen(req) File "/usr/lib/python2.6/urllib2.py", li

我是Django的新手,在将数据发布到Django Web应用程序时遇到一些问题

在django方面,我有一个表单(由couchdb-couchdbkit-django-ext支持)。 在客户端,我使用urllib2批量发布数据(通过数据列表逐个发布)

在一条记录(一次post传递)之后,它得到下面的消息,再次运行下一行传递,我再次得到此消息:

       res = urllib2.urlopen(req)
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 397, in open
    response = meth(req, response)
  File "/usr/lib/python2.6/urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.6/urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
  urllib2.HTTPError: HTTP Error 500: INTERNAL SERVER ERROR
服务器说:

    Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 284, in run
    self.finish_response()
  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 324, in finish_response
    self.write(data)
  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 420, in write
    self._write(data)
  File "/usr/lib/python2.6/socket.py", line 318, in write
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 59571)
    self.flush()
  File "/usr/lib/python2.6/socket.py", line 297, in flush
    self._sock.sendall(buffer(data, write_offset, buffer_size))
error: [Errno 104] Connection reset by peer
Traceback (most recent call last):
  File "/usr/lib/python2.6/SocketServer.py", line 283, in _handle_request_noblock
----------------------------------------
    self.process_request(request, client_address)
  File "/usr/lib/python2.6/SocketServer.py", line 309, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.6/SocketServer.py", line 322, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 570, in __init__
    BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  File "/usr/lib/python2.6/SocketServer.py", line 618, in __init__
    self.finish()
  File "/usr/lib/python2.6/SocketServer.py", line 661, in finish
    self.wfile.flush()
  File "/usr/lib/python2.6/socket.py", line 297, in flush
    self._sock.sendall(buffer(data, write_offset, buffer_size))
error: [Errno 32] Broken pipe
我必须声明我正在开发嵌入式django开发web服务器

这是测试的输出:

======================================================================
ERROR: test_create_object (objects.tests.ObjectAppTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/patrick/workspace_py/objects/tests.py", line 21, in test_create_object
    rs = c.post('/objects/create/', param)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 455, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 256, in post
    return self.request(**r)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 387, in request
    response = self.handler(environ)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 84, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 169, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 218, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/coffin/views/defaults.py", line 34, in server_error
    content = render_to_string(template_name, Context({}))
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/coffin/template/loader.py", line 50, in render_to_string
    template = get_template(template_name)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/coffin/template/loader.py", line 24, in get_template
    return env.get_template(template_name)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/loaders.py", line 377, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: 500.html

----------------------------------------------------------------------
有人知道怎么修理吗


Patrick

有时我发现使用development server(如您的日志)时出现奇怪的错误和快速连接重置。如果你的应用程序是关于http、REST和webservices的,你应该使用Apache或Gunicorn,这样你在开发时会感觉到一个更真实的环境。

问题似乎出在你的服务器代码上,而不是客户端。事实上,客户端只是说服务器返回了一个错误代码。您可能希望尝试/捕获服务器代码以查看该端的异常。或者使用单元测试,您可以使用测试客户端(
self.client
,在测试用例中)执行与使用urllib类似的操作。我添加了服务器日志。我将编写一个测试。这些stacktraces看起来像是一个客户在阅读发送给他的内容之前离开的。如果未提升默认日志级别,则应仅在控制台中看到GET/objects/create/500。您应该在django视图中添加一个try/catch,例如import traceback traceback.print_exc()。或者尝试单元测试:)我同时添加了测试输出,我将添加您建议的try/catch块……这是否意味着嵌入式服务器不稳定?Cherrypy对于开发来说已经足够稳定了,并且正是为了实现这一点。坚持到错误解决为止。Django不使用cherrypy作为开发服务器。@patoch,假设它没有涵盖所有场景。如果您的架构足够大,您应该在更类似于生产环境的环境中进行早期测试,并且不要忘记在应用程序的编码会话中使用DEBUG=False运行一次。
======================================================================
ERROR: test_create_object (objects.tests.ObjectAppTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/patrick/workspace_py/objects/tests.py", line 21, in test_create_object
    rs = c.post('/objects/create/', param)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 455, in post
    response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 256, in post
    return self.request(**r)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 387, in request
    response = self.handler(environ)
  File "/usr/local/lib/python2.6/dist-packages/django/test/client.py", line 84, in __call__
    response = self.get_response(request)
  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 169, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 218, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/coffin/views/defaults.py", line 34, in server_error
    content = render_to_string(template_name, Context({}))
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/coffin/template/loader.py", line 50, in render_to_string
    template = get_template(template_name)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/coffin/template/loader.py", line 24, in get_template
    return env.get_template(template_name)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/environment.py", line 719, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/environment.py", line 693, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/patrick/workspace_py/env/lib/python2.6/site-packages/jinja2/loaders.py", line 377, in get_source
    raise TemplateNotFound(template)
TemplateNotFound: 500.html

----------------------------------------------------------------------