FlASK中的endpoint问题

先贴一点有关的flask代码,时间有限,我慢慢扩充

以下是flask源码中app.py中add_url_rule的代码。

主要是view_func  -- endpoint -- url 之间的对应关系。

flask中,view_func与url并不是直接对应的,是url先找到endpoint, 然后通过endpoint再去找到对应的view_func,一个endpoint只能对应于一个view_func,在注册add_url_rule的时候,如果不指定endpoint,那么endpoint就会默认为函数名字,如果同一个endpoint于多个url注册的话,会有问题,详见代码中,会判断之前已经对应到的跟现在是不是一个,如果不是的话,那么就要抛出异常。然后再去访问这些url当然是肯定不行的啦。有时间会慢慢扩充这部分的内容。

 1     @setupmethod
 2     def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
 3         if endpoint is None:
 4             endpoint = _endpoint_from_view_func(view_func)
 5         options['endpoint'] = endpoint
 6         methods 
 7        if methods is None:
 8             methods = getattr(view_func, 'methods', None) or ('GET',)
 9         if isinstance(methods, string_types):
10             raise TypeError('Allowed methods have to be iterables of strings, '
11                             'for example: @app.route(..., methods=["POST"])')
12         methods = set(item.upper() for item in methods)
13 
14         required_methods = set(getattr(view_func, 'required_methods', ()))
15 
16         provide_automatic_options = getattr(view_func,
17             'provide_automatic_options', None)
18 
19         if provide_automatic_options is None:
20             if 'OPTIONS' not in methods:
21                 provide_automatic_options = True
22                 required_methods.add('OPTIONS')
23             else:
24                 provide_automatic_options = False
25 
26         # Add the required methods now.
27         methods |= required_methods
28 
29         rule = self.url_rule_class(rule, methods=methods, **options)
30         rule.provide_automatic_options = provide_automatic_options
31 
32         self.url_map.add(rule)
33         if view_func is not None:
34             old_func = self.view_functions.get(endpoint)
35             if old_func is not None and old_func != view_func:
36                 raise AssertionError('View function mapping is overwriting an '
37                                      'existing endpoint function: %s' % endpoint)
38             self.view_functions[endpoint] = view_func

 

posted on 2015-12-07 03:20 symons 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/symons1992/p/5024912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在划线处完成SampleApp工程应用层初始化函数代码的注释(用文简述各段代码)。 void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; // #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES // #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to start the device. ZDOInitDevice(0); #endif // SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast; SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; // SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; // SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc=(SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); // Register for all key events - This app will handle all key events RegisterForKeys( SampleApp_TaskID ); // By default, all devices start out in Group 1 SampleApp_Group.ID = 0x0001; osal_memcpy( SampleApp_Group.name, "Group 1", 7 ); aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); }
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值