跳到主要內容

12.DRF-節流

Django rest framework源碼分析(3)----節流


添加節流


自定義節流的方法



  • 限制60s內只能訪問3次


(1)API文件夾下面新建throttle.py,代碼如下:


# utils/throttle.py

from rest_framework.throttling import BaseThrottle
import time
VISIT_RECORD = {} #保存訪問記錄

class VisitThrottle(BaseThrottle):
'''60s內只能訪問3次'''
def __init__(self):
self.history = None #初始化訪問記錄

def allow_request(self,request,view):
#獲取用戶ip (get_ident)
remote_addr = self.get_ident(request)
ctime = time.time()
#如果當前IP不在訪問記錄裏面,就添加到記錄
if remote_addr not in VISIT_RECORD:
VISIT_RECORD[remote_addr] = [ctime,] #鍵值對的形式保存
return True #True表示可以訪問
#獲取當前ip的歷史訪問記錄
history = VISIT_RECORD.get(remote_addr)
#初始化訪問記錄
self.history = history

#如果有歷史訪問記錄,並且最早一次的訪問記錄離當前時間超過60s,就刪除最早的那個訪問記錄,
#只要為True,就一直循環刪除最早的一次訪問記錄
while history and history[-1] < ctime - 60:
history.pop()
#如果訪問記錄不超過三次,就把當前的訪問記錄插到第一個位置(pop刪除最後一個)
if len(history) < 3:
history.insert(0,ctime)
return True

def wait(self):
'''還需要等多久才能訪問'''
ctime = time.time()
return 60 - (ctime - self.history[-1])

(2)settings中全局配置節流


#全局
REST_FRAMEWORK = {
#節流
"DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.VisitThrottle'],
}

(3)現在訪問auth看看結果:



  • 60s內訪問次數超過三次,會限制訪問

  • 提示剩餘多少時間可以訪問



接着訪問



節流源碼分析


(1)dispatch


def dispatch(self, request, *args, **kwargs):
"""
`.dispatch()` is pretty much the same as Django's regular dispatch,
but with extra hooks for startup, finalize, and exception handling.
"""
self.args = args
self.kwargs = kwargs
#對原始request進行加工,豐富了一些功能
#Request(
# request,
# parsers=self.get_parsers(),
# authenticators=self.get_authenticators(),
# negotiator=self.get_content_negotiator(),
# parser_context=parser_context
# )
#request(原始request,[BasicAuthentications對象,])
#獲取原生request,request._request
#獲取認證類的對象,request.authticators
#1.封裝request
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate?

try:
#2.認證
self.initial(request, *args, **kwargs)

# Get the appropriate handler method
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed

response = handler(request, *args, **kwargs)

except Exception as exc:
response = self.handle_exception(exc)

self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response

(2)initial


def initial(self, request, *args, **kwargs):
"""
Runs anything that needs to occur prior to calling the method handler.
"""
self.format_kwarg = self.get_format_suffix(**kwargs)

# Perform content negotiation and store the accepted info on the request
neg = self.perform_content_negotiation(request)
request.accepted_renderer, request.accepted_media_type = neg

# Determine the API version, if versioning is in use.
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme

# Ensure that the incoming request is permitted
#4.實現認證
self.perform_authentication(request)
#5.權限判斷
self.check_permissions(request)
#6.控制訪問頻率
self.check_throttles(request)

(3)check_throttles


裏面有個allow_request


def check_throttles(self, request):
"""
Check if request should be throttled.
Raises an appropriate exception if the request is throttled.
"""
for throttle in self.get_throttles():
if not throttle.allow_request(request, self):
self.throttled(request, throttle.wait())

(4)get_throttles


def get_throttles(self):
"""
Instantiates and returns the list of throttles that this view uses.
"""
return [throttle() for throttle in self.throttle_classes]

(5)thtottle_classes



內置節流類


上面是寫的自定義節流,drf內置了很多節流的類,用起來比較方便。


(1)BaseThrottle



  • 自己要寫allow_request和wait方法

  • get_ident就是獲取ip


class BaseThrottle(object):
"""
Rate throttling of requests.
"""

def allow_request(self, request, view):
"""
Return `True` if the request should be allowed, `False` otherwise.
"""
raise NotImplementedError('.allow_request() must be overridden')

def get_ident(self, request):
"""
Identify the machine making the request by parsing HTTP_X_FORWARDED_FOR
if present and number of proxies is > 0. If not use all of
HTTP_X_FORWARDED_FOR if it is available, if not use REMOTE_ADDR.
"""
xff = request.META.get('HTTP_X_FORWARDED_FOR')
remote_addr = request.META.get('REMOTE_ADDR')
num_proxies = api_settings.NUM_PROXIES

if num_proxies is not None:
if num_proxies == 0 or xff is None:
return remote_addr
addrs = xff.split(',')
client_addr = addrs[-min(num_proxies, len(addrs))]
return client_addr.strip()

return ''.join(xff.split()) if xff else remote_addr

def wait(self):
"""
Optionally, return a recommended number of seconds to wait before
the next request.
"""
return None

(2)SimpleRateThrottle


class SimpleRateThrottle(BaseThrottle):
"""
A simple cache implementation, that only requires `.get_cache_key()`
to be overridden.

The rate (requests / seconds) is set by a `rate` attribute on the View
class. The attribute is a string of the form 'number_of_requests/period'.

Period should be one of: ('s', 'sec', 'm', 'min', 'h', 'hour', 'd', 'day')

Previous request information used for throttling is stored in the cache.
"""
cache = default_cache
timer = time.time
cache_format = 'throttle_%(scope)s_%(ident)s'
scope = None #這個值自定義,寫什麼都可以
THROTTLE_RATES = api_settings.DEFAULT_THROTTLE_RATES

def __init__(self):
if not getattr(self, 'rate', None):
self.rate = self.get_rate()
self.num_requests, self.duration = self.parse_rate(self.rate)

def get_cache_key(self, request, view):
"""
Should return a unique cache-key which can be used for throttling.
Must be overridden.

May return `None` if the request should not be throttled.
"""
raise NotImplementedError('.get_cache_key() must be overridden')

def get_rate(self):
"""
Determine the string representation of the allowed request rate.
"""
if not getattr(self, 'scope', None):
msg = ("You must set either `.scope` or `.rate` for '%s' throttle" %
self.__class__.__name__)
raise ImproperlyConfigured(msg)

try:
return self.THROTTLE_RATES[self.scope]
except KeyError:
msg = "No default throttle rate set for '%s' scope" % self.scope
raise ImproperlyConfigured(msg)

def parse_rate(self, rate):
"""
Given the request rate string, return a two tuple of:
<allowed number of requests>, <period of time in seconds>
"""
if rate is None:
return (None, None)
num, period = rate.split('/')
num_requests = int(num)
duration = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}[period[0]]
return (num_requests, duration)

def allow_request(self, request, view):
"""
Implement the check to see if the request should be throttled.

On success calls `throttle_success`.
On failure calls `throttle_failure`.
"""
if self.rate is None:
return True

self.key = self.get_cache_key(request, view)
if self.key is None:
return True

self.history = self.cache.get(self.key, [])
self.now = self.timer()

# Drop any requests from the history which have now passed the
# throttle duration
while self.history and self.history[-1] <= self.now - self.duration:
self.history.pop()
if len(self.history) >= self.num_requests:
return self.throttle_failure()
return self.throttle_success()

def throttle_success(self):
"""
Inserts the current request's timestamp along with the key
into the cache.
"""
self.history.insert(0, self.now)
self.cache.set(self.key, self.history, self.duration)
return True

def throttle_failure(self):
"""
Called when a request to the API has failed due to throttling.
"""
return False

def wait(self):
"""
Returns the recommended next request time in seconds.
"""
if self.history:
remaining_duration = self.duration - (self.now - self.history[-1])
else:
remaining_duration = self.duration

available_requests = self.num_requests - len(self.history) + 1
if available_requests <= 0:
return None

return remaining_duration / float(available_requests)

我們可以通過繼承SimpleRateThrottle類,來實現節流,會更加的簡單,因為SimpleRateThrottle裏面都幫我們寫好了


(1)throttle.py


from rest_framework.throttling import SimpleRateThrottle

class VisitThrottle(SimpleRateThrottle):
'''匿名用戶60s只能訪問三次(根據ip)'''
scope = 'NBA' #這裏面的值,自己隨便定義,settings裏面根據這個值配置Rate

def get_cache_key(self, request, view):
#通過ip限制節流
return self.get_ident(request)

class UserThrottle(SimpleRateThrottle):
'''登錄用戶60s可以訪問10次'''
scope = 'NBAUser' #這裏面的值,自己隨便定義,settings裏面根據這個值配置Rate

def get_cache_key(self, request, view):
return request.user.username

(2)settings.py


#全局
REST_FRAMEWORK = {
#節流
"DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.UserThrottle'], #全局配置,登錄用戶節流限制(10/m)
"DEFAULT_THROTTLE_RATES":{
'NBA':'3/m', #沒登錄用戶3/m,NBA就是scope定義的值
'NBAUser':'10/m', #登錄用戶10/m,NBAUser就是scope定義的值
}
}

(3)views.py


局部配置方法


class AuthView(APIView):
.
.
.
# 默認的節流是登錄用戶(10/m),AuthView不需要登錄,這裏用匿名用戶的節流(3/m)
throttle_classes = [VisitThrottle,]   . .

# views.py
from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from rest_framework.views import APIView
from API import models
from rest_framework.request import Request
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from API.utils.permission import SVIPPremission,MyPremission
from API.utils.throttle import VisitThrottle

ORDER_DICT = {
1:{
'name':'apple',
'price':15
},
2:{
'name':'dog',
'price':100
}
}

def md5(user):
import hashlib
import time
#當前時間,相當於生成一個隨機的字符串
ctime = str(time.time())
m = hashlib.md5(bytes(user,encoding='utf-8'))
m.update(bytes(ctime,encoding='utf-8'))
return m.hexdigest()


class AuthView(APIView):
'''用於用戶登錄驗證'''

authentication_classes = [] #裏面為空,代表不需要認證
permission_classes = [] #不裏面為空,代表不需要權限
# 默認的節流是登錄用戶(10/m),AuthView不需要登錄,這裏用匿名用戶的節流(3/m)
throttle_classes = [VisitThrottle,]

def post(self,request,*args,**kwargs):
ret = {'code':1000,'msg':None}
try:
user = request._request.POST.get('username')
pwd = request._request.POST.get('password')
obj = models.UserInfo.objects.filter(username=user,password=pwd).first()
if not obj:
ret['code'] = 1001
ret['msg'] = '用戶名或密碼錯誤'
#為用戶創建token
token = md5(user)
#存在就更新,不存在就創建
models.UserToken.objects.update_or_create(user=obj,defaults={'token':token})
ret['token'] = token
except Exception as e:
ret['code'] = 1002
ret['msg'] = '請求異常'
return JsonResponse(ret)


class OrderView(APIView):
'''
訂單相關業務(只有SVIP用戶才能看)
'''

def get(self,request,*args,**kwargs):
self.dispatch
#request.user
#request.auth
ret = {'code':1000,'msg':None,'data':None}
try:
ret['data'] = ORDER_DICT
except Exception as e:
pass
return JsonResponse(ret)


class UserInfoView(APIView):
'''
訂單相關業務(普通用戶和VIP用戶可以看)
'''
permission_classes = [MyPremission,] #不用全局的權限配置的話,這裏就要寫自己的局部權限
def get(self,request,*args,**kwargs):

print(request.user)
return HttpResponse('用戶信息')

說明:



  • API.utils.throttle.UserThrottle 這個是全局配置(根據ip限制,10/m)

  • DEFAULT_THROTTLE_RATES --->>>設置訪問頻率的

  • throttle_classes = [VisitThrottle,] --->>>局部配置(不適用settings裏面默認的全局配置)


總結


基本使用



  • 創建類,繼承BaseThrottle, 實現:allow_request ,wait

  • 創建類,繼承SimpleRateThrottle, 實現: get_cache_key, scope='NBA' (配置文件中的key)


全局


   #節流
"DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.UserThrottle'], #全局配置,登錄用戶節流限制(10/m)
"DEFAULT_THROTTLE_RATES":{
'NBA':'3/m', #沒登錄用戶3/m,NBA就是scope定義的值
'NBAUser':'10/m', #登錄用戶10/m,NBAUser就是scope定義的值
}
}

局部


throttle_classes = [VisitThrottle,]

所有代碼


認證、權限和節流


# MyProject/urls.py

from django.contrib import admin
from django.urls import path
from API.views import AuthView,OrderView,UserInfoView

urlpatterns = [
path('admin/', admin.site.urls),
path('api/v1/auth/',AuthView.as_view()),
path('api/v1/order/',OrderView.as_view()),
path('api/v1/info/',UserInfoView.as_view()),
]

#全局 settings.py
REST_FRAMEWORK = {
#認證
"DEFAULT_AUTHENTICATION_CLASSES":['API.utils.auth.Authentication',],
#權限
"DEFAULT_PERMISSION_CLASSES":['API.utils.permission.SVIPPermission'],
#節流
"DEFAULT_THROTTLE_CLASSES":['API.utils.throttle.UserThrottle'], #全局配置,登錄用戶節流限制(10/m)
"DEFAULT_THROTTLE_RATES":{
'NBA':'3/m', #沒登錄用戶3/m,NBA就是scope定義的值
'NBAUser':'10/m', #登錄用戶10/m,NBAUser就是scope定義的值
}
}

# API/models.py


from django.db import models

class UserInfo(models.Model):
USER_TYPE = (
(1,'普通用戶'),
(2,'VIP'),
(3,'SVIP')
)

user_type = models.IntegerField(choices=USER_TYPE)
username = models.CharField(max_length=32)
password = models.CharField(max_length=64)

class UserToken(models.Model):
user = models.OneToOneField(UserInfo,on_delete=models.CASCADE)
token = models.CharField(max_length=64)

# API/views.py

from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from rest_framework.views import APIView
from API import models
from rest_framework.request import Request
from rest_framework import exceptions
from rest_framework.authentication import BaseAuthentication
from API.utils.permission import SVIPPermission,MyPermission
from API.utils.throttle import VisitThrottle

ORDER_DICT = {
1:{
'name':'apple',
'price':15
},
2:{
'name':'dog',
'price':100
}
}

def md5(user):
import hashlib
import time
#當前時間,相當於生成一個隨機的字符串
ctime = str(time.time())
m = hashlib.md5(bytes(user,encoding='utf-8'))
m.update(bytes(ctime,encoding='utf-8'))
return m.hexdigest()


class AuthView(APIView):
'''用於用戶登錄驗證'''

authentication_classes = [] #裏面為空,代表不需要認證
permission_classes = [] #不裏面為空,代表不需要權限
# 默認的節流是登錄用戶(10/m),AuthView不需要登錄,這裏用匿名用戶的節流(3/m)
throttle_classes = [VisitThrottle,]

def post(self,request,*args,**kwargs):
ret = {'code':1000,'msg':None}
try:
user = request._request.POST.get('username')
pwd = request._request.POST.get('password')
obj = models.UserInfo.objects.filter(username=user,password=pwd).first()
if not obj:
ret['code'] = 1001
ret['msg'] = '用戶名或密碼錯誤'
#為用戶創建token
token = md5(user)
#存在就更新,不存在就創建
models.UserToken.objects.update_or_create(user=obj,defaults={'token':token})
ret['token'] = token
except Exception as e:
ret['code'] = 1002
ret['msg'] = '請求異常'
return JsonResponse(ret)


class OrderView(APIView):
'''
訂單相關業務(只有SVIP用戶才能看)
'''

def get(self,request,*args,**kwargs):
self.dispatch
#request.user
#request.auth
ret = {'code':1000,'msg':None,'data':None}
try:
ret['data'] = ORDER_DICT
except Exception as e:
pass
return JsonResponse(ret)


class UserInfoView(APIView):
'''
訂單相關業務(普通用戶和VIP用戶可以看)
'''
permission_classes = [MyPermission,] #不用全局的權限配置的話,這裏就要寫自己的局部權限
def get(self,request,*args,**kwargs):

print(request.user)
return HttpResponse('用戶信息')

# API/utils/auth/py

from rest_framework import exceptions
from API import models
from rest_framework.authentication import BaseAuthentication


class Authentication(BaseAuthentication):
'''用於用戶登錄驗證'''
def authenticate(self,request):
token = request._request.GET.get('token')
token_obj = models.UserToken.objects.filter(token=token).first()
if not token_obj:
raise exceptions.AuthenticationFailed('用戶認證失敗')
#在rest framework內部會將這兩個字段賦值給request,以供後續操作使用
return (token_obj.user,token_obj)

def authenticate_header(self, request):
pass

# utils/permission.py

from rest_framework.permissions import BasePermission

class SVIPPermission(BasePermission):
message = "必須是SVIP才能訪問"
def has_permission(self,request,view):
if request.user.user_type != 3:
return False
return True


class MyPermission(BasePermission):
def has_permission(self,request,view):
if request.user.user_type == 3:
return False
return True

# utils/throttle.py
#
# from rest_framework.throttling import BaseThrottle
# import time
# VISIT_RECORD = {} #保存訪問記錄
#
# class VisitThrottle(BaseThrottle):
# '''60s內只能訪問3次'''
# def __init__(self):
# self.history = None #初始化訪問記錄
#
# def allow_request(self,request,view):
# #獲取用戶ip (get_ident)
# remote_addr = self.get_ident(request)
# ctime = time.time()
# #如果當前IP不在訪問記錄裏面,就添加到記錄
# if remote_addr not in VISIT_RECORD:
# VISIT_RECORD[remote_addr] = [ctime,] #鍵值對的形式保存
# return True #True表示可以訪問
# #獲取當前ip的歷史訪問記錄
# history = VISIT_RECORD.get(remote_addr)
# #初始化訪問記錄
# self.history = history
#
# #如果有歷史訪問記錄,並且最早一次的訪問記錄離當前時間超過60s,就刪除最早的那個訪問記錄,
# #只要為True,就一直循環刪除最早的一次訪問記錄
# while history and history[-1] < ctime - 60:
# history.pop()
# #如果訪問記錄不超過三次,就把當前的訪問記錄插到第一個位置(pop刪除最後一個)
# if len(history) < 3:
# history.insert(0,ctime)
# return True
#
# def wait(self):
# '''還需要等多久才能訪問'''
# ctime = time.time()
# return 60 - (ctime - self.history[-1])

from rest_framework.throttling import SimpleRateThrottle

class VisitThrottle(SimpleRateThrottle):
'''匿名用戶60s只能訪問三次(根據ip)'''
scope = 'NBA' #這裏面的值,自己隨便定義,settings裏面根據這個值配置Rate

def get_cache_key(self, request, view):
#通過ip限制節流
return self.get_ident(request)

class UserThrottle(SimpleRateThrottle):
'''登錄用戶60s可以訪問10次'''
scope = 'NBAUser' #這裏面的值,自己隨便定義,settings裏面根據這個值配置Rate

def get_cache_key(self, request, view):
return request.user.username
本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】



※教你寫出一流的銷售文案?



※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益



※回頭車貨運收費標準



※別再煩惱如何寫文案,掌握八大原則!



※超省錢租車方案



※產品缺大量曝光嗎?你需要的是一流包裝設計!



Orignal From: 12.DRF-節流

留言

這個網誌中的熱門文章

Python 併發總結,多線程,多進程,異步IO

1 測量函數運行時間 import time def profile(func): def wrapper(*args, ** kwargs): import time start = time.time() func( *args, ** kwargs) end = time.time() print ' COST: {} ' .format(end - start) return wrapper @profile def fib(n): if n<= 2 : return 1 return fib(n-1) + fib(n-2 ) fib( 35 )   2 啟動多個線程,並等待完成   2.1 使用threading.enumerate() import threading for i in range(2 ): t = threading.Thread(target=fib, args=(35 ,)) t.start() main_thread = threading.currentThread() for t in threading.enumerate(): if t is main_thread: continue t.join()   2.2 先保存啟動的線程 threads = [] for i in range(5 ): t = Thread(target=foo, args= (i,)) threads.append(t) t.start() for t in threads: t.join()   3 使用信號量,限制同時能有幾個線程訪問臨界區 from threading import Semaphore import time sema = Semaphor...

高雄十大包子名店出爐

, 圖文:吳恩文 高雄包子大賽落幕了,我只能就我個人意見, 介紹一下前十名這些包子,但是不能代表其他四位評審的意見,雖然身為評審長,我通常不會第一個表示意見,以免影響其他評審, 我主要工作是負責發問。   這次參賽的素包子很少,而且都不夠細致,又偏油,我不愛, 但是第一名的甜芝麻包-熔岩黑金包,竟然是素食得名- 漢來蔬食巨蛋店。   這包子賣相太好,竹炭粉的黑色外皮刷上金粉,一上桌,眾人驚呼, 搶拍照,內餡是芝麻餡,混一點花生醬增稠,加入白糖芝麻油, 熔岩爆漿的程度剛剛好,我一直以為芝麻要配豬油才行、 但是選到好的黑芝麻油一樣不減香醇, 當下有二位評審就想宅配回家。   尤其特別的是,黑芝麻餡室溫易化,師傅必須要輪班躲在冷藏室內, 穿著大外套才能包,一天包不了多少,我笑說,漢來美食,集團餐廳那麼多,實力雄厚,根本是「 奧運選手報名參加村裡運動會」嘛,其他都是小包子店啊, 但是沒辦法,顯然大家都覺得它好看又好吃, 目前限定漢來蔬食高雄巨蛋店,二顆88元,可以冷凍宅配, 但是要排一陣子,因為供不應求,聽說,四月份, 台北sogo店開始會賣。   第二名的包子,左營寬來順早餐店,顯然平易近人的多,一顆肉包, 十塊錢,是所有參賽者中最便宜的,當然,個頭也小, 它的包子皮明顯和其他不同,灰灰的老麵,薄但紮實有嚼勁, 肉餡新鮮帶汁,因為打了些水,味道極其簡單,就是蔥薑,塩, 香油,薑味尤其明顯,是老眷村的味道, 而特別的是老闆娘是台灣本省人, 當年完全是依據眷村老兵的口味一步一步調整而來,沒有加什麼糖、 五香粉,胡椒粉,油蔥酥。就是蔥薑豬肉和老麵香,能得名, 應該是它的平實無華,鮮美簡單,打動人心。   這是標準的心靈美食,可以撫慰人心,得名之前,寛來順已經天天排隊,現在,恐怕要排更久了, 建議大家六七點早點上門。   第三名,「專十一」很神奇,我記得比賽最後, 大家連吃了幾家不能引起共鳴的包子,有些累,到了專十一, 就坐著等包子,其他評審一吃,就催我趕快試,我一吃, 也醒了大半。   它的包子皮厚薄適中,但是高筋麵粉高些,老麵加一點點酵母, 我心中,它的皮屬一屬二,至於餡又多又好吃,蛋黃還是切丁拌入, 不是整顆放,吃起來「美味、均衡、飽滿」。一顆二十元。   老闆是陸軍專科十一期畢業取名專十一,...

韋伯連續劇終於更新 期待第一季順利完結

  地球天文學界的跳票大王詹姆斯·韋伯空間望遠鏡 (James Webb Space Telescope,縮寫為 JWST)自 1996 年以來斷斷續續不按劇本演出的連續劇終於讓焦慮的觀眾們又等到了一次更新:五層遮陽罩測試順利完成。 裝配完成的韋伯望遠鏡與好夥伴遮陽罩同框啦。Credit: NASA   嚴格的測試是任何空間任務順利成功的重中之重。遮陽罩,這個韋伯望遠鏡異常重要的親密夥伴,要是無法正常運轉的話,韋伯的這一季天文界連續劇說不準就要一直拖更了。   詹姆斯·韋伯空間望遠鏡是歷史上造出的最先進的空間望遠鏡。它不僅是一架紅外望遠鏡,還具有特別高的靈敏度。但想要達到辣么高的靈敏度來研究系外行星和遙遠的宇宙童年,韋伯童鞋必須非常"冷靜",體溫升高的話,靈敏度會大大折損。這個時候,遮陽罩就要大顯身手啦。   遮陽罩在韋伯的設計中至關重要。韋伯望遠鏡會被發射到拉格朗日 L2 點,運行軌道很高,遠離太陽、地球與月球。太陽是韋伯的主要熱量干擾的來源,其次是地球與月球。遮陽罩會有效阻斷來自這三大熱源的能量並保護韋伯維持在工作溫度正常運轉。這個工作溫度指的是零下 220 攝氏度(-370 華氏度;50 開爾文)。 上圖中我們可以看出,韋伯望遠鏡的配置大致可分為兩部分:紅色較熱的一面溫度為 85 攝氏度,藍色較冷的一面溫度達到零下 233 攝氏度。紅色的這部分中,儀器包括太陽能板、通信設備、計算機、以及轉向裝置。藍色部分的主要裝置包括鏡面、探測器、濾光片等。Credit: STSci.   遮陽罩的那一部分和望遠鏡的鏡面這部分可以產生非常極端的溫差。遮陽的這面溫度可以達到 110 攝氏度,足以煮熟雞蛋,而背陰處的部分溫度極低,足以凍結氧氣。   工程師們剛剛完成了五層遮陽罩的測試,按照韋伯在 L2 時的運行狀態安裝了遮陽罩。L2 距離地球約 160 萬公里。NASA 表示這些測試使用了航天器的自帶系統來展開遮陽罩,測試目前都已成功完成。韋伯望遠鏡遮陽罩負責人 James Cooper 介紹說這是遮陽罩"第一次在望遠鏡系統的电子設備的控制下展開。儘管這個任務非常艱巨,難度高,但測試順利完成,遮陽罩展開時的狀態非常驚艷"。   遮陽罩由五層 Kapton 製成。Kapton 是一種聚酰亞胺薄膜材料, 耐高溫絕...