本帖最后由 阿奇_o 于 2022-11-30 20:02 编辑
你是在做那个"选做题"吧~? 如果是,那么具体场景应该是 处理request的字符串数据,
这包括GET的params的query-string形式,和POST的data字符串形式(其实也是类似query-string的形式)等
所以,我举个例子吧,大概应该是这样(你改成pytest的测试用例那样的test_xxx,该改的则改,"能跑就行" ^_ ):class Request:
def __init__(self, method='GET', url='/'):
self._method = method
self._url = url
self._params = None
@property
def method(self):
return self._method.lower()
@method.setter
def method(self, value):
self._method = value
@property
def url(self):
return self._url.lstrip('/')
@url.setter
def url(self, url):
self._url = url
@property
def params(self):
return self._params if self._params else None
@params.setter
def params(self, params=None):
if params is None:
self._params = dict()
else:
if isinstance(params, dict):
self._params = params
elif isinstance(params, str):
'''like the Query-String: /search?id=1001&name=Alice '''
kv = params[params.find('?')+1:].split('&')
self._params = {i.split('=')[0] : i.split('=')[1] for i in kv}
req = Request()
print('url: ', req.url)
print('Method: ', req.method)
print('params: ', req.params)
req.url = '/search?id=1001&name=Alice' # query-string
print('url: ', req.url)
print('Method: ', req.method)
print('params: ', req.params)
# req.data = '/register?name=Alice&password=123456' # POST的
|