python web开发框架 - 之 Django Request

1 篇文章 0 订阅
1 篇文章 0 订阅
使用例子:  
Python代码   收藏代码
  1. >>> from django.http import *  
  2. >>> request = HttpRequest()  
  3. >>> request.__doc__  
  4. >>> help(request)  
  5. >>> request.META  
  6. 29: {}  
  7. >>> request.is_ajax()  
  8. 30False  
  9. >>> request.GET  
  10. 31: {}  
  11. >>> request.FILES  
  12. 32: {}  



以下是request 的源码:  
Python代码   收藏代码
  1. class HttpRequest(object):  
  2.     """A basic HTTP request."""  
  3.   
  4.     # The encoding used in GET/POST dicts. None means use default setting.  
  5.     _encoding = None  
  6.     _upload_handlers = []  
  7.   
  8.     def __init__(self):  
  9.         self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}  
  10.         self.path = ''  
  11.         self.path_info = ''  
  12.         self.method = None  
  13.   
  14.     def __repr__(self):  
  15.         return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \  
  16.             (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES),  
  17.             pformat(self.META))  
  18.   
  19.     def get_host(self):  
  20.         """Returns the HTTP host using the environment or request headers."""  
  21.         # We try three options, in order of decreasing preference.  
  22.         if 'HTTP_X_FORWARDED_HOST' in self.META:  
  23.             host = self.META['HTTP_X_FORWARDED_HOST']  
  24.         elif 'HTTP_HOST' in self.META:  
  25.             host = self.META['HTTP_HOST']  
  26.         else:  
  27.             # Reconstruct the host using the algorithm from PEP 333.  
  28.             host = self.META['SERVER_NAME']  
  29.             server_port = str(self.META['SERVER_PORT'])  
  30.             if server_port != (self.is_secure() and '443' or '80'):  
  31.                 host = '%s:%s' % (host, server_port)  
  32.         return host  
  33.   
  34.     def get_full_path(self):  
  35.         return ''  
  36.   
  37.     def build_absolute_uri(self, location=None):  
  38.         """ 
  39.         Builds an absolute URI from the location and the variables available in 
  40.         this request. If no location is specified, the absolute URI is built on 
  41.         ``request.get_full_path()``. 
  42.         """  
  43.         if not location:  
  44.             location = self.get_full_path()  
  45.         if not absolute_http_url_re.match(location):  
  46.             current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http',  
  47.                                          self.get_host(), self.path)  
  48.             location = urljoin(current_uri, location)  
  49.         return iri_to_uri(location)  
  50.   
  51.     def is_secure(self):  
  52.         return os.environ.get("HTTPS") == "on"  
  53.   
  54.     def is_ajax(self):  
  55.         return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'  
  56.   
  57.     def _set_encoding(self, val):  
  58.         """ 
  59.         Sets the encoding used for GET/POST accesses. If the GET or POST 
  60.         dictionary has already been created, it is removed and recreated on the 
  61.         next access (so that it is decoded correctly). 
  62.         """  
  63.         self._encoding = val  
  64.         if hasattr(self'_get'):  
  65.             del self._get  
  66.         if hasattr(self'_post'):  
  67.             del self._post  
  68.   
  69.     def _get_encoding(self):  
  70.         return self._encoding  
  71.   
  72.     encoding = property(_get_encoding, _set_encoding)  
  73.   
  74.     def _initialize_handlers(self):  
  75.         self._upload_handlers = [uploadhandler.load_handler(handler, self)  
  76.                                  for handler in settings.FILE_UPLOAD_HANDLERS]  
  77.   
  78.     def _set_upload_handlers(self, upload_handlers):  
  79.         if hasattr(self'_files'):  
  80.             raise AttributeError("You cannot set the upload handlers after the upload has been processed.")  
  81.         self._upload_handlers = upload_handlers  
  82.   
  83.     def _get_upload_handlers(self):  
  84.         if not self._upload_handlers:  
  85.             # If thre are no upload handlers defined, initialize them from settings.  
  86.             self._initialize_handlers()  
  87.         return self._upload_handlers  
  88.   
  89.     upload_handlers = property(_get_upload_handlers, _set_upload_handlers)  
  90.   
  91.     def parse_file_upload(self, META, post_data):  
  92.         """Returns a tuple of (POST QueryDict, FILES MultiValueDict)."""  
  93.         self.upload_handlers = ImmutableList(  
  94.             self.upload_handlers,  
  95.             warning = "You cannot alter upload handlers after the upload has been processed."  
  96.         )  
  97.         parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)  
  98.         return parser.parse()  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值