django 不同项目见跨域代理的设置

url: url(r'^wapi/', include('proxy.wapi_urls',namespace='wapi')),

wapi_urls:url(r'^',proxy_iapi,kwargs={'domain':settings.WAPI_PROXY_DOMAIN}),

    

def proxy_iapi(request,**kwargs):

    print('进入代理')

      try:

        if 'multipart/form-data' in request.META.get('CONTENT_TYPE',''):#文件上传request.FILES

            return multiPost(request,**kwargs)

        conn = httplib.HTTPConnection(kwargs.get('domain',None) or IAPI_PROXY_DOMAIN)

        log.debug('request.method:%s'%request.method)

        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

转载于:https://my.oschina.net/u/2603728/blog/807550

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值