提交 097c7b7b 编写于 作者: F feilong

修订Python到第6章

上级 2f33bb37
{
"one_line": {
"ErrorCode.internal_ret_2_http(error_code)": [
"str(error_code)"
],
"ErrorCode(Enum)": [
"ErrorCode(object)"
],
"ret['err'].name.lower()": [
"ret['err'].name"
]
},
"source": "error_code.py",
"source": "error_code.md",
"depends": [],
"exercise_id": 153,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# 错误码处理
编写一个错误码枚举,支持转换成字符串格式方法: internal_ret_2_http
```python
# -*- coding: UTF-8 -*-
from enum import Enum
class ErrorCode(Enum):
# TODO(You): 请在此定义错误码
@staticmethod
def internal_ret_2_http(ret):
ret['err'] = ret['err'].name.lower()
if __name__ == '__main__':
ret = {'err': ErrorCode.NOT_FOUND}
ErrorCode.internal_ret_2_http(ret)
assert ret['err'] == 'not_found'
```
以下对错误码定义 **正确** 的是?。
## template
```python
from enum import Enum
class ErrorCode(Enum):
SUCCESS = 0
FAILED = 1
NOT_FOUND = 2
ALREADY_EXIST = 3
INVALID_PARAMETERS = 4
@staticmethod
def internal_ret_2_http(ret):
ret['err'] = ret['err'].name.lower()
if __name__ == '__main__':
ret = {'err': ErrorCode.NOT_FOUND}
ErrorCode.internal_ret_2_http(ret)
assert ret['err'] == 'not_found'
```
## 答案
```python
class ErrorCode(Enum):
SUCCESS = 0
FAILED = 1
NOT_FOUND = 2
ALREADY_EXIST = 3
INVALID_PARAMETERS = 4
```
## 选项
### A
```python
class ErrorCode(Enum):
SUCCESS: 0
FAILED: 1
NOT_FOUND: 2
ALREADY_EXIST: 3
INVALID_PARAMETERS: 4
```
### B
```python
class ErrorCode(Enum):
SUCCESS
FAILED
NOT_FOUND
ALREADY_EXIST
INVALID_PARAMETERS
```
### C
```python
class ErrorCode(Enum):
'SUCCESS' = 0
'FAILED' = 1
'NOT_FOUND' = 2
'ALREADY_EXIST' = 3
'INVALID_PARAMETERS' = 4
```
{
"one_line": {
"req_path is None": [
"req_path is not None"
],
"self.routes.get(req_path) is None": [
"self.routes.get(req_path) is not None"
],
"handler(req), 200": [
"self.routes.get(req_path)(req), 503"
],
"lambda req:": [
"lambda path, data:"
]
},
"source": "router.py",
"source": "router.md",
"depends": [
"error_code.py"
],
"exercise_id": 154,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# 路由器
下面是一个 HTTP 状态代码的定义:
* 2xx:成功
* 200 正常,请求已完成。
* 201 正常,紧接POST命令。
* 202 正常,已接受用于处理,但处理尚未完成。
* 203 正常,部分信息—返回的信息只是一部分。
* 204 正常,无响应—已接收请求,但不存在要回送的信息。
* 3xx:重定向
* 301 已移动,请求的数据具有新的位置且更改是永久的。
* 302 已找到,请求的数据临时具有不同 URI。
* 303 请参阅其它,可在另一 URI 下找到对请求的响应,且应使用 GET 方法检索此响应。
* 304 未修改,未按预期修改文档。
* 305 使用代理,必须通过位置字段中提供的代理来访问请求的资源。
* 306 未使用,不再使用,保留此代码以便将来使用。
* 4xx:客户机中出现的错误
* 400 错误请求,请求中有语法问题,或不能满足请求。
* 401 未授权,未授权客户机访问数据。
* 402 需要付款,表示计费系统已有效。
* 403 禁止,即使有授权也不需要访问。
* 404 找不到,服务器找不到给定的资源;文档不存在。
* 407 代理认证请求,客户机首先必须使用代理认证自身。
* 415 介质类型不受支持,服务器拒绝服务请求,因为不支持请求实体的格式。
* 5xx:服务器中出现的错误
* 500 内部错误,因为意外情况,服务器不能完成请求。
* 501 未执行,服务器不支持请求的工具。
* 502 错误网关,服务器接收到来自上游服务器的无效响应。
* 503 无法获得服务,由于临时过载或维护,服务器无法处理请求。
编写一个路由服务,支持注入路由配置,正确处理请求参数
```python
# -*- coding: UTF-8 -*-
from error_code import ErrorCode
import json
import logging
import traceback
logger = logging.getLogger(__name__)
class Router:
def __init__(self, routes) -> None:
self.routes = routes
def dispatch(self, http_request_str):
# TODO(You): 请实现路由逻辑,返回符合语义的HTTP状态码
if __name__ == '__main__':
# 注册路由
router = Router({
'/': lambda req: print(f'goto home:{str(req)}')
})
# 分发路由
router.dispatch(json.dumps({
'path': '/',
'data': ["Hello", "World!"]
}))
```
以下路由的逻辑代码都一样,请选出**HTTP状态码符合语义**的选项。
## template
```python
from error_code import ErrorCode
import json
import logging
import traceback
logger = logging.getLogger(__name__)
class Router:
def __init__(self, routes) -> None:
self.routes = routes
def dispatch(self, http_request_str):
''' 实现一个路由器
http_request_str: 请求数据字符串,包含 {path:"",data:""}格式的JSON字符串
'''
assert http_request_str is not None
assert type(http_request_str) == type('')
req_path = None
req = None
try:
http_request = json.loads(http_request_str)
req_path = http_request.get('path')
req = http_request.get('data')
except Exception as e:
logger.error(f"parse data exception:{str(e)}")
return {'err': ErrorCode.INVALID_PARAMETERS}, 200
if req_path is None or self.routes.get(req_path) is None:
return {'err': ErrorCode.NOT_FOUND}, 404
try:
handler = self.routes.get(req_path)
return handler(req), 200
except Exception as e:
logger.error(f"route to '{req_path}' exception:{str(e)}")
logger.error(traceback.format_exc())
return {'err': ErrorCode.FAILED}, 500
if __name__ == '__main__':
server = Router({
'/': lambda req: print(f'goto home:{str(req)}')
})
server.dispatch(json.dumps({
'path': '/',
'data': ["Hello", "World!"]
}))
```
## 答案
```python
class Router:
def dispatch(self, http_request_str):
req_path = None
req = None
try:
http_request = json.loads(http_request_str)
req_path = http_request.get('path')
req = http_request.get('data')
except Exception as e:
logger.error(f"parse data exception:{str(e)}")
return {'err': ErrorCode.INVALID_PARAMETERS}, 400
if req_path is None or self.routes.get(req_path) is None:
return {'err': ErrorCode.NOT_FOUND}, 404
try:
handler = self.routes.get(req_path)
return handler(req), 200
except Exception as e:
logger.error(f"route to '{req_path}' exception:{str(e)}")
logger.error(traceback.format_exc())
return {'err': ErrorCode.FAILED}, 500
```
## 选项
### A
```python
class Router:
def dispatch(self, http_request_str):
req_path = None
req = None
try:
http_request = json.loads(http_request_str)
req_path = http_request.get('path')
req = http_request.get('data')
except Exception as e:
logger.error(f"parse data exception:{str(e)}")
return {'err': ErrorCode.INVALID_PARAMETERS}, 200
if req_path is None or self.routes.get(req_path) is None:
return {'err': ErrorCode.NOT_FOUND}, 200
try:
handler = self.routes.get(req_path)
return handler(req), 200
except Exception as e:
logger.error(f"route to '{req_path}' exception:{str(e)}")
logger.error(traceback.format_exc())
return {'err': ErrorCode.FAILED}, 200
```
### B
```python
class Router:
def dispatch(self, http_request_str):
req_path = None
req = None
try:
http_request = json.loads(http_request_str)
req_path = http_request.get('path')
req = http_request.get('data')
except Exception as e:
logger.error(f"parse data exception:{str(e)}")
return {'err': ErrorCode.INVALID_PARAMETERS}, 301
if req_path is None or self.routes.get(req_path) is None:
return {'err': ErrorCode.NOT_FOUND}, 404
try:
handler = self.routes.get(req_path)
return handler(req), 200
except Exception as e:
logger.error(f"route to '{req_path}' exception:{str(e)}")
logger.error(traceback.format_exc())
return {'err': ErrorCode.FAILED}, 500
```
### C
```python
class Router:
def dispatch(self, http_request_str):
req_path = None
req = None
try:
http_request = json.loads(http_request_str)
req_path = http_request.get('path')
req = http_request.get('data')
except Exception as e:
logger.error(f"parse data exception:{str(e)}")
return {'err': ErrorCode.INVALID_PARAMETERS}, 200
if req_path is None or self.routes.get(req_path) is None:
return {'err': ErrorCode.NOT_FOUND}, 200
try:
handler = self.routes.get(req_path)
return handler(req), 200
except Exception as e:
logger.error(f"route to '{req_path}' exception:{str(e)}")
logger.error(traceback.format_exc())
return {'err': ErrorCode.FAILED}, 500
```
{
"one_line": {
"'/kv/create': self.__create": [
"'/kv/create': self.__remove"
],
"ErrorCode.internal_ret_2_http(ret)": [
"ret.internal_ret_2_http()"
],
"self.validator.validate(req, ['key', 'value'])": [
"self.validator.validate(req, ['key'])"
]
},
"source": "server.py",
"source": "server.md",
"depends": [
"error_code.py",
"store.py",
......@@ -18,5 +7,7 @@
"validator.py"
],
"exercise_id": 152,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# Python Web 服务模拟器
综合使用前2节的ErrorCode、Router两个类,模拟一个 Web 服务,支持:
1. 创建资源
2. 删除资源
3. 统计资源个数
那么,先创建一个资源,接着删除同一个资源,最后统计资源个数,总数应该为0
```python
# -*- coding: UTF-8 -*-
from error_code import ErrorCode
from store import Store
from router import Router
from validator import KeyValueValidator
import json
import logging
import traceback
logger = logging.getLogger(__name__)
class App:
def __init__(self) -> None:
self.store = Store({})
self.validator = KeyValueValidator()
# 创建一个路由器,支持3个目标API
self.router = Router({
'/': lambda req: self.__home(req),
'/kv/create': lambda req: self.__create(req),
'/kv/remove': lambda req: self.__remove(req),
'/kv/count': lambda req: self.__count(req),
})
def post(self, path, data):
'''HTTP POST方法模拟实现
path: 请求路径
data: 请求数据,使用JSON模拟
'''
# TODO(You): 请正确实现 post 方法,接受 API 请求
return resp, status_code
def __home(self, req):
# 首页
return {'err': ErrorCode.SUCCESS, 'result': "Welcome!"}
def __create(self, req):
# 创建资源
ret = self.validator.validate(req, ['key', 'value'])
if ret['err'] != ErrorCode.SUCCESS:
return ret
return self.store.create(req['key'], req['value'])
def __remove(self, req):
# 移除资源
ret = self.validator.validate(req, ['key', 'condition'])
if ret['err'] != ErrorCode.SUCCESS:
return ret
return self.store.remove(req['key'], req['condition'])
def __count(self, req):
# 统计资源个数
return self.store.count()
if __name__ == '__main__':
app = App()
resp, status = app.post('/kv/create', {
'key': 'test',
'value': 1000,
})
resp, status = app.post('/kv/remove', {
'key': 'test',
'condition': 1000,
})
resp, status = app.post('/kv/count', {})
assert status == 200
assert json.loads(resp)['err'] == 'success'
assert json.loads(resp)['result'] == 0
```
以下对 post 方法的实现,**正确并且最佳**的代码是?
## template
```python
from error_code import ErrorCode
from store import Store
from router import Router
from validator import KeyValueValidator
import json
import logging
import traceback
logger = logging.getLogger(__name__)
class App:
def __init__(self) -> None:
self.store = Store({})
self.validator = KeyValueValidator()
self.router = Router({
'/': self.__home,
'/kv/create': self.__create,
'/kv/remove': self.__remove,
'/kv/count': self.__count,
})
def post(self, path, data):
http_request = {
'path': path,
'data': data
}
ret, status_code = self.router.dispatch(json.dumps(http_request))
ErrorCode.internal_ret_2_http(ret)
resp = ""
try:
resp = json.dumps(ret)
except Exception as e:
logger.error("parse resp exception:%s", str(e))
logger.error(traceback.format_exc())
return resp, status_code
def __home(self, req):
return {'err': ErrorCode.SUCCESS, 'result': "Welcome!"}
def __create(self, req):
ret = self.validator.validate(req, ['key', 'value'])
if ret['err'] != ErrorCode.SUCCESS:
return ret
return self.store.create(req['key'], req['value'])
def __remove(self, req):
ret = self.validator.validate(req, ['key', 'condition'])
if ret['err'] != ErrorCode.SUCCESS:
return ret
return self.store.remove(req['key'], req['condition'])
def __count(self, req):
return self.store.count()
if __name__ == '__main__':
app = App()
resp, status = app.post('/kv/create', {
'key': 'test',
'value': 1000,
})
resp, status = app.post('/kv/remove', {
'key': 'test',
'condition': 1000,
})
resp, status = app.post('/kv/count', {})
assert status == 200
assert json.loads(resp)['err'] == 'success'
assert json.loads(resp)['result'] == 0
```
## 答案
```python
class App:
...
def post(self, path, data):
http_request = {
'path': path,
'data': data
}
ret, status_code = self.router.dispatch(json.dumps(http_request))
ErrorCode.internal_ret_2_http(ret)
resp = ""
try:
resp = json.dumps(ret)
except Exception as e:
logger.error("parse resp exception:%s", str(e))
logger.error(traceback.format_exc())
return resp, status_code
```
## 选项
### A
```python
class App:
...
def post(self, path, data):
http_request = {
'path': path,
'data': data
}
ret, status_code = self.router.dispatch(http_request)
ErrorCode.internal_ret_2_http(ret)
resp = ""
try:
resp = json.dumps(ret)
except Exception as e:
logger.error("parse resp exception:%s", str(e))
logger.error(traceback.format_exc())
return resp, status_code
```
### B
```python
class App:
...
def post(self, path, data):
http_request = {
'path': path,
'data': data
}
ret, status_code = self.router.dispatch(json.dumps(http_request))
resp = ""
try:
resp = json.dumps(ret)
except Exception as e:
logger.error("parse resp exception:%s", str(e))
logger.error(traceback.format_exc())
return resp, status_code
```
### C
```python
class App:
...
def post(self, path, data):
http_request = {
'path': path,
'data': data
}
ret, status_code = self.router.dispatch(json.dumps(http_request))
ErrorCode.internal_ret_2_http(ret)
resp = json.dumps(ret)
return resp, status_code
```
{
"one_line": {
"self.records.get(key) is None": [
"self.records.get(key) is not None"
],
"'result': len(self.records.keys())": [
"'result': len(self.records)"
],
"self.records.get(key) == condition": [
"self.records.get(key) != condition"
]
},
"source": "store.py",
"source": "store.md",
"depends": [
"error_code.py"
],
"exercise_id": 150,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# 状态存储
使用dict数据结构,实现创建、删除、查询总数存储接口,创建/删除同一个资源后,总是应该为0
```python
# -*- coding: UTF-8 -*-
from error_code import ErrorCode
import logging
logger = logging.getLogger(__name__)
class Store:
def __init__(self, config) -> None:
self.config = config
self.records = {}
def __where(self, key, condition):
if condition is None:
return True
return self.records.get(key) == condition
def create(self, key, value):
if self.records.get(key) is None:
self.records[key] = value
return {'err': ErrorCode.SUCCESS}
else:
return {'err': ErrorCode.ALREADY_EXIST}
def update(self, key, value, condition=None):
if self.__where(key, condition):
self.records[key] = value
return {'err': ErrorCode.SUCCESS}
else:
return {'err': ErrorCode.NOT_FOUND}
def remove(self, key, condition=None):
if self.__where(key, condition):
del self.records[key]
return {'err': ErrorCode.SUCCESS}
else:
return {'err': ErrorCode.NOT_FOUND}
def count(self):
return {'err': ErrorCode.SUCCESS, 'result': len(self.records.keys())}
def fetch(self, key, condition=None):
# TODO(You): 请实现一个根据key 和条件 condition 查找的方法
if __name__ == '__main__':
store = Store({})
ret = store.create("test", 100)
assert ret['err'] == ErrorCode.SUCCESS
ret = store.remove("test")
assert ret['err'] == ErrorCode.SUCCESS
ret = store.count()
assert ret['err'] == ErrorCode.SUCCESS
assert ret['result'] == 0
```
请选出下列能**正确**实现这一功能的选项。
## template
```python
from error_code import ErrorCode
import logging
logger = logging.getLogger(__name__)
class Store:
def __init__(self, config) -> None:
self.config = config
self.records = {}
def __where(self, key, condition):
if condition is None:
return True
return self.records.get(key) == condition
def create(self, key, value):
if self.records.get(key) is None:
self.records[key] = value
return {'err': ErrorCode.SUCCESS}
else:
return {'err': ErrorCode.ALREADY_EXIST}
def update(self, key, value, condition=None):
if self.__where(key, condition):
self.records[key] = value
return {'err': ErrorCode.SUCCESS}
else:
return {'err': ErrorCode.NOT_FOUND}
def remove(self, key, condition=None):
if self.__where(key, condition):
del self.records[key]
return {'err': ErrorCode.SUCCESS}
else:
return {'err': ErrorCode.NOT_FOUND}
def count(self):
return {'err': ErrorCode.SUCCESS, 'result': len(self.records.keys())}
def fetch(self, key, condition=None):
if self.__where(key, condition):
result = self.records.get(key)
if result is None:
return {'err': ErrorCode.NOT_FOUND}
else:
return {'err': ErrorCode.SUCCESS, 'result': [result]}
else:
return {'err': ErrorCode.NOT_FOUND}
if __name__ == '__main__':
store = Store({})
ret = store.create("test", 100)
assert ret['err'] == ErrorCode.SUCCESS
ret = store.remove("test")
assert ret['err'] == ErrorCode.SUCCESS
ret = store.count()
assert ret['err'] == ErrorCode.SUCCESS
assert ret['result'] == 0
```
## 答案
```python
class Store:
...
def fetch(self, key, condition=None):
if self.__where(key, condition):
result = self.records.get(key)
if result is None:
return {'err': ErrorCode.NOT_FOUND}
else:
return {'err': ErrorCode.SUCCESS, 'result': [result]}
else:
return {'err': ErrorCode.NOT_FOUND}
```
## 选项
### A
```python
class Store:
...
def fetch(self, key, condition=None):
result = self.records.get(key)
if result is None:
return {'err': ErrorCode.NOT_FOUND}
else:
return {'err': ErrorCode.SUCCESS, 'result': [result]}
```
### B
```python
class Store:
...
def fetch(self, key, condition=None):
if self.__where(key, condition):
result = self.records.get(key)
return {'err': ErrorCode.SUCCESS, 'result': [result]}
else:
return {'err': ErrorCode.NOT_FOUND}
```
### C
```python
class Store:
...
def fetch(self, key, condition=None):
if self.__where(key, condition):
result = self.records[key]
if result is None:
return {'err': ErrorCode.NOT_FOUND}
else:
return {'err': ErrorCode.SUCCESS, 'result': [result]}
else:
return {'err': ErrorCode.NOT_FOUND}
```
{
"one_line": {
"\"key\": {\"type\": \"string\"},": [
"\"key\": {\"type\": \"number\"},"
],
"\"required\": required": [
"\"required\": req"
],
"\"value\": {\"type\": \"number\"}": [
"\"value\": {\"type\": [\"number\",\"string\"]}"
]
},
"source": "validator.py",
"source": "validator.md",
"depends": [
"error_code.py"
],
"exercise_id": 151,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# 参数校验
使用 jsonschema 校验参数,key:字符串类型, value:数字, condition: 数字
```python
# -*- coding: UTF-8 -*-
from error_code import ErrorCode
from jsonschema import validate
import json
import logging
import traceback
logger = logging.getLogger(__name__)
class KeyValueValidator:
def __init__(self) -> None:
pass
def validate(self, req, required):
'''使用jsonschema校验参数
req: 请求参数
required: 必须要有的字段
'''
# TODO(You): 请正确配置 jsonschema
schema = ...
try:
validate(instance=req, schema=schema)
return {
'err': ErrorCode.SUCCESS
}
except Exception as e:
logger.error(f"validate exception:{str(e)}")
logger.error(traceback.format_exc())
return {
'err': ErrorCode.INVALID_PARAMETERS
}
if __name__ == '__main__':
v = KeyValueValidator()
ret = v.validate({'key': "test", 'value': 100}, ['key', 'value'])
assert ret['err'] == ErrorCode.SUCCESS
ret = v.validate({'key': "test"}, ['key', 'value'])
assert ret['err'] == ErrorCode.INVALID_PARAMETERS
```
以下对方法 validate 里 jsonschema 配置正确的是?
## template
```python
from error_code import ErrorCode
from jsonschema import validate
import json
import logging
import traceback
logger = logging.getLogger(__name__)
class KeyValueValidator:
def __init__(self) -> None:
pass
def validate(self, req, required):
schema = {
"type": "object",
"properties": {
"key": {"type": "string"},
"value": {"type": "number"},
"condition": {"type": "number"},
},
"required": required
}
try:
validate(instance=req, schema=schema)
return {
'err': ErrorCode.SUCCESS
}
except Exception as e:
logger.error(f"validate exception:{str(e)}")
logger.error(traceback.format_exc())
return {
'err': ErrorCode.INVALID_PARAMETERS
}
if __name__ == '__main__':
v = KeyValueValidator()
ret = v.validate({'key': "test", 'value': 100}, ['key', 'value'])
assert ret['err'] == ErrorCode.SUCCESS
ret = v.validate({'key': "test"}, ['key', 'value'])
assert ret['err'] == ErrorCode.INVALID_PARAMETERS
```
## 答案
```python
class KeyValueValidator:
...
def validate(self, req, required):
schema = {
"type": "object",
"properties": {
"key": {"type": "string"},
"value": {"type": "number"},
"condition": {"type": "number"},
},
"required": required
}
...
```
## 选项
### A
```python
class KeyValueValidator:
...
def validate(self, req, required):
schema = {
"properties": {
"key": {"type": "string"},
"value": {"type": "number"},
"condition": {"type": "number"},
},
"required": required
}
...
```
### B
```python
class KeyValueValidator:
...
def validate(self, req, required):
schema = {
"type": "object",
"properties": {
"key": {"type": "string"},
"value": {"type": "number"},
"condition": {"type": "number"},
}
}
...
```
### C
```python
class KeyValueValidator:
...
def validate(self, req, required):
schema = {
"type": "object",
"properties": {
"key": {"type": "number"},
"value": {"type": "number"},
"condition": {"type": "number"},
},
"required": required
}
...
```
{
"one_line": {
"pip install django": [
"pip install dango"
],
"django-admin": [
"django_admin"
],
"startproject": [
"project"
],
"startapp": [
"app"
]
},
"source": "howto.py",
"source": "howto.md",
"depends": [],
"exercise_id": 104,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# Python Django 框架
自己动手,丰衣足食,打印 Django 教程的入口信息
```python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
# TODO(You): 请在此添加创建django项目和初始化目录的命令
create_django = ...
init_django = ...
installs = [
"安装:pip install django",
f"创建django项目命令:{create_django}",
"进入目录:cd projectName",
f"初始化django项目命令:{init_django}",
"进一步查看教程:https://docs.djangoproject.com/zh-hans/3.2/"
]
for step in installs:
print(step)
```
以下正确赋值创建django项目和初始化目录的命令的代码是?
## template
```python
if __name__ == '__main__':
installs = [
"安装:pip install django",
"初始化:django-admin startproject projectName",
"进入目录:cd projectName",
"初始化:python manage.py startapp projectApp",
"进一步查看教程:https://docs.djangoproject.com/zh-hans/3.2/"
]
for step in installs:
print(step)
```
## 答案
```python
create_django = 'django-admin startproject projectName'
init_django = 'python manage.py startapp projectApp'
```
## 选项
### A
```python
create_django = 'django startproject projectName'
init_django = 'django startapp projectApp'
```
### B
```python
create_django = 'python manage.py startproject projectName'
init_django = 'python manage.py startapp projectApp'
```
### C
```python
create_django = 'django-admin startproject projectName'
init_django = 'django-admin startapp projectApp'
```
{
"one_line": {
"start()": [
"listen()",
"accept()"
],
"app.listen(8000)": [
"app.listen(80)"
],
"self.write": [
"self.read"
]
},
"source": "hello.py",
"source": "hello.md",
"depends": [],
"exercise_id": 106,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# Python tornado 框架
tornado 服务,极简路由
```python
# -*- coding: UTF-8 -*-
import tornado.web
import tornado.ioloop
class IndexHandler(tornado.web.RequestHandler):
"""主路由处理类"""
def get(self):
"""对应http的get请求方式"""
# TODO(You): 实现Tornado get 方法
if __name__ == "__main__":
app = tornado.web.Application([
(r"/", IndexHandler),
])
app.listen(8000)
print("* Tornado Web Server 已在 8000 端口启动。")
print("* 请在浏览器里输入 127.0.0.1:8000")
tornado.ioloop.IOLoop.current().start()
```
请选出下列能**正确**实现这一功能的选项。
## template
```python
import tornado.web
import tornado.ioloop
class IndexHandler(tornado.web.RequestHandler):
"""主路由处理类"""
def get(self):
"""对应http的get请求方式"""
self.write("Hello Tornado!")
if __name__ == "__main__":
app = tornado.web.Application([
(r"/", IndexHandler),
])
app.listen(8000)
print("* Tornado Web Server 已在 8000 端口启动。")
print("* 请在浏览器里输入 127.0.0.1:8000")
tornado.ioloop.IOLoop.current().start()
```
## 答案
```python
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello Tornado!")
```
## 选项
### A
```python
class IndexHandler(tornado.web.RequestHandler):
def get(self):
return "Hello Tornado!"
```
### B
```python
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello Tornado!", 200)
```
### C
```python
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write_error("Hello Tornado!", 500)
```
{
"one_line": {
"pywsgi.WSGIServer((host, port), app)": [
"pywsgi.WSGIServer(host, port, app)"
],
"server.serve_forever()": [
"server.start()"
],
"methods=['GET']": [
"methods=[GET]"
]
},
"source": "server.py",
"source": "server.md",
"depends": [],
"exercise_id": 212,
"type": "code_options"
"type": "code_options",
"author": "huanhuilong",
"notebook_enable": true
}
\ No newline at end of file
# Flask简单使用
用flask启动web服务,响应根页面HTTP GET请求:`'/'`,返回`"<p>Hello World!</p>"`
```python
# -*- coding: UTF-8 -*-
import sys
from flask import Flask
from flask_cors import CORS
from gevent import pywsgi, monkey
monkey.patch_all()
app = Flask(
__name__,
static_folder='web',
static_url_path=''
)
def after_request(resp):
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
app.after_request(after_request)
app.config['JSON_AS_ASCII'] = False
CORS(app, supports_credentials=True)
# TODO(You): 请在此添加Flask API响应
if __name__ == '__main__':
host = '127.0.0.1'
port = 1024
print('@启动服务...')
print("@本地调试:http://{}:{}".format(host, port))
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
app.run(host=host, port=port)
else:
server = pywsgi.WSGIServer((host, port), app)
server.serve_forever()
```
请选出下列能**正确**实现这一功能的选项。
## template
```python
import sys
from flask import Flask
from flask_cors import CORS
from gevent import pywsgi, monkey
monkey.patch_all()
app = Flask(
__name__,
static_folder='web',
static_url_path=''
)
def after_request(resp):
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
app.after_request(after_request)
app.config['JSON_AS_ASCII'] = False
CORS(app, supports_credentials=True)
@app.route('/', methods=['GET'])
def home():
return "<p>Hello World!</p>"
if __name__ == '__main__':
host = '127.0.0.1'
port = 1024
print('@启动服务...')
print("@本地调试:http://{}:{}".format(host, port))
if len(sys.argv) > 1 and sys.argv[1] == 'debug':
app.run(host=host, port=port)
else:
server = pywsgi.WSGIServer((host, port), app)
server.serve_forever()
```
## 答案
```python
@app.route('/', methods=['GET'])
def home():
return "<p>Hello World!</p>"
```
## 选项
### A
```python
@app.route('/', methods=['POST'])
def home():
return "<p>Hello World!</p>"
```
### B
```python
@app.route('/', methods=['GET'])
def home():
return "Hello World!"
```
### C
```python
@app.route('/')
def home():
return "<p>Hello World!</p>"
```
......@@ -9,5 +9,5 @@ if __name__ == '__main__':
# walker = TreeWalker("data", "python", "python")
# walker.walk()
md = MDWalker('data/2.python中阶/1.基本技能')
md = MDWalker('data/2.python中阶/2.Web应用开发')
md.walk()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册