这是我根据Ry4an接受的答案,实际使用的解决方案是。 django HttpRequest
对象可以作为wsgirequest
完全可以直接传递给hgweb_mod.run_wsgi
,但我无法弄清楚返回的内容。以下为我工作,它当然不处理所有的边缘情况,但我能够推动和拉我的回购这种方式:
#!/usr/bin/python
class WsgiWrapper(object):
"""
A wrapper object that works reasonably well (at least in the cases in which it is currently being used)
to turn a django request into a Wsgi thingy.
"""
def __init__(self, request):
self.request = request
self.status = None
self.headers = None
self.exc_info = None
def __call__(self, status=None, response_headers=None, exc_info=None):
if status is not None:
self.status = status
self.headers = response_headers
self.exc_info = exc_info
self.outputbuffer = cStringIO.StringIO()
return self.outputbuffer.write
def serve_hg(request, repopath):
"""
Delegates to mercurial's built-in hgweb WSGI application to serve up access
to mercurial HTTPCommand Protocol.
"""
#Put together a wsgi request from the django request.
wrapper = WsgiWrapper(request)
env = {}
env.update(request.META)
env['SCRIPT_NAME'] = '/'
env['PATH_INFO'] = '/'
req = hgweb.request.wsgirequest(env, wrapper)
#Invoke hgweb as a wsgi application.
hgw = hgweb.hgweb(repopath)
hgw.repo.baseui.setconfig('web', 'allow_push', '*')
hgw.repo.baseui.setconfig('web', 'push_ssl', 'false')
gen = hgw.run_wsgi(req)
data = wrapper.outputbuffer.getvalue()
if len(data):
gen = [data]
wrapper.outputbuffer.close()
#Now build up the django response object from that.
resp = HttpResponse("".join(gen))
stat = wrapper.status
if stat is None:
resp.status_code = 200
else:
pair = stat.split(' ', 1)
resp.status_code = pair[0]
if len(pair) > 1:
resp.reason_phrase = pair[1]
hopbyhop = (
'Connection',
'Keep-Alive',
'Proxy-Authenticate',
'Proxy-Authorization',
'TE',
'Trailers',
'Transfer-Encoding',
'Upgrade',
)
if wrapper.headers is not None:
for k, v in wrapper.headers:
if k not in hopbyhop:
resp[k] = v
return resp
这真棒,谢谢!只要我可以测试它并验证它的工作,我会接受这个答案。 – brianmearns
它不会像那样工作,但它不会太远。请记住,这是与GPL代码的“链接”,因此您可以使用它,并且可以将其作为Web服务提供,然后将其作为GPL发布,但如果不受“GPL”条款的约束,则无法“分发”它GPL。 –
谢谢,我很喜欢GPL =)。我不清楚如何正确地将一个单独的WSGI应用程序(hgweb)集成到django中:我不认为Django的请求对象(它传递给视图函数)实际上是一个WSGI请求,它可以传递给'run_wsgi' 。有任何想法吗? – brianmearns