url: url(r'^wapi/', include('proxy.wapi_urls',namespace='wapi')),json
wapi_urls:url(r'^',proxy_iapi,kwargs={'domain':settings.WAPI_PROXY_DOMAIN}),api
def proxy_iapi(request,**kwargs):cookie
print('進入代理')app
try:dom
if 'multipart/form-data' in request.META.get('CONTENT_TYPE',''):#文件上傳request.FILESpost
return multiPost(request,**kwargs)url
conn = httplib.HTTPConnection(kwargs.get('domain',None) or IAPI_PROXY_DOMAIN)spa
log.debug('request.method:%s'%request.method)debug
conn.request(request.method,create_url(request,getChannelId(request)),body=create_body(request),headers=create_header(request))代理
iapi_resp=conn.getresponse()
iapi_data=iapi_resp.read()
conn.close()
resp=HttpResponse(iapi_data,status=iapi_resp.status,content_type=iapi_resp.getheader('content-type', 'application/json; charset=utf-8'))
log.debug('in proxy_iapi done!')
except:
resp=HttpResponseNotFound('kaoke')
log.error('error',exc_info=traceback)
return resp
def create_body(request):
body=request.body
if body:
log.debug('nobody.post:%s',request.POST)
#log.debug('body:%s'%body)
else:
req_post = request.POST.dict()
if req_post:
for key in req_post.keys():
value=req_post[key].encode('utf-8')
req_post[key]=value
body = urllib.urlencode(req_post)
else:
body=''
try:
pass
except:
log.error('error',exc_info=traceback)
body = ''
# log.debug(vars(body))
return body
def create_header(request):
print('kaishi')
headers = {}
try:
if request.META.get('CONTENT_LENGTH',''):
headers.update({'Content-Length':request.META.get('CONTENT_LENGTH',0)})
headers.update({'Content-Type':request.META.get('CONTENT_TYPE','')})
log.debug("crete_header:cookie %s"%(request.META.get('HTTP_COOKIE','')))
headers.update({'Cookie':request.META.get('HTTP_COOKIE','')})
except:
log.error('error',exc_info=traceback)
return headers
def create_url(request,channel_id):
url = ''
try:
urlpath=request.path
param=request.META.get('QUERY_STRING','')
log.debug('param_1:%s'%param)
log.debug('in channel_id:%s'%channel_id)
# if param and param.has_key('channel_id'):
# del param['channel_id']
# log.debug('param_2:%s'%param)
# url = '%s?%s%s'%(urlpath,'channel_id=%s'%(channel_id or 'None'),param and '&%s'%(param) or '')
c_id = request.GET.get('channel_id',None)
if channel_id:
if c_id or c_id == '':
if c_id != channel_id:
param = param.replace('channel_id=%s'%c_id,'channel_id=%s'%channel_id)
url = '%s?%s'%(urlpath,param)
else:
url = '%s?%s'%(urlpath,param)
else:
url = '%s?%s%s'%(urlpath,'channel_id=%s'%(channel_id,),param and '&%s'%(param) or '')
else:
url = '%s?%s'%(urlpath,param)
except:
log.error('error',exc_info=traceback)
log.debug('url:%s',url)
return url
def multiPost(request,**kwargs):
url = kwargs.get('domain',None) or IAPI_PROXY_DOMAIN
h = httplib.HTTPConnection(url)
log.debug(dir(h))
h.putrequest('POST', create_url(request, getChannelId(request)))
headers = create_header(request)
# log.debug('headers:%s'%headers)
for k, v in headers.items():
h.putheader(k, v)
h.endheaders()
log.debug('before create_body')
h.send(create_body(request))
iapi_resp = h.getresponse()
log.debug(iapi_resp)
iapi_data = iapi_resp.read()
log.debug(iapi_data)
h.close()
resp = HttpResponse(iapi_data, status=iapi_resp.status,content_type=iapi_resp.getheader('content-type', 'application/json; charset=utf-8'))
return resp
def getChannelId(request):
c_id = request.GET.get('channel_id',CHANNEL_ID)
return c_id and c_id or CHANNEL_ID