diff --git a/works/OLDERHARD/M2.1/M2.1.txt b/works/OLDERHARD/M2.1/M2.1.txt new file mode 100644 index 0000000..b78eec2 --- /dev/null +++ b/works/OLDERHARD/M2.1/M2.1.txt @@ -0,0 +1,31 @@ +HTTP 协议的基本组成部分包括: + +HTTP 方法:定义了对特定资源执行的操作。常见的 HTTP 方法包括 GET、POST、PUT、DELETE 等。 + +URL(Uniform Resource Locator):用于标识要访问的资源的地址。 + +HTTP 头部:包含了请求或响应的元数据信息,如内容类型、内容长度、授权信息等。 + +HTTP 主体:可选的,用于传输数据,如 POST 请求中的表单数据。 + +状态码:在响应中指示请求的处理状态,如 200 OK、404 Not Found 等。 + + +HTTP 请求中传参的方式包括: + +查询字符串参数:通过在 URL 的末尾附加参数来传递数据,格式为 '?key1=value1&key2=value2'。 + + 请求头部:在 HTTP 头部中添加自定义的字段来传递参数,例如 'Content-Type: application/json'。 + +3请求主体:主要用于 POST、PUT 请求,将数据作为主体传输,可以是表单数据、JSON 数据等。 + +URL 路径参数:在 URL 中通过路径的方式传递参数,例如 '/users/123' 中的 '123' 就是路径参数。 + +这些都是常见的 HTTP 请求传参方式,选择哪种方式取决于实际需求和约定。HTTP 请求的灵活性和可扩展性使得开发者可以根据需要选择最合适的方式来传递参数。 + + + +Flask 是一个轻量级的 Web 框架,用于构建 Web 应用程序。它提供了路由、请求处理、模板渲染、数据库连接等功能,使开发 Web 应用变得更加简单和灵活。 + +本次作业的流程: +Flask 接收请求,调用本地的 Python 脚本,执行查询逻辑,响应给浏览器。 diff --git a/works/OLDERHARD/M2.1/app.py b/works/OLDERHARD/M2.1/app.py new file mode 100644 index 0000000..51bdb9c --- /dev/null +++ b/works/OLDERHARD/M2.1/app.py @@ -0,0 +1,75 @@ +from flask import Flask,request,jsonify,Response +from fake_useragent import UserAgent +import requests +import json +url="https://codeforces.com/api/user.info" +headers={ + 'user-Agent':UserAgent().random +} +def solve(name): + params = { + "handles": name + } + try: + response=requests.get(url=url,params=params,headers=headers) + status_code =response.status_code + if status_code==200: # 此项 handle 可以查询到 + Json=json.loads(response.text) + user=Json['result'][0] + if 'rating' in user: + return { + 'success':True, + 'result':{ + 'handle':user['handle'], + 'rating':int(user['rating']), + 'rank':user['rank'] + } + } + else: + return { + 'success':True, + 'result':{ + 'handle':user['handle'] + } + } + elif status_code==400: # 此项 handle 无法找到 + return { + 'success':False, + 'type':1, + 'message':'no such handle' + } + else: # 在查询此项时遭遇异常 HTTP 响应 + return { + 'success':False, + 'type':2, + 'message':'HTTP response with code {}'.format(status_code) + } + except requests.exceptions.ConnectionError or requests.exceptions.RequestException as e: # 在查询此项时未收到有效 HTTP 响应 + return { + 'success':False, + 'type':3, + 'message':'Request timeout' + } + except : # 在查询此项时程序发生运行时异常 + return { + 'success':False, + 'type':4, + 'message':'Internal Server Error' + } + + +app=Flask(__name__) # 定义一个Flask的实例 + +@app.route('/') # 装饰器,将 URL 路径映射到下面的处理函数 +def query(): + handles= request.args.get('handles').split(',') + results=[] + for handle in handles: + result= solve(handle) + results.append(result) + return Response(json.dumps(results),mimetype='application/json') # 返回 JSON 格式的查询结果 + + +if __name__=='__main__': + app.run(host='127.0.0.1',debug=True,port=2333) # Flask监听 IP地址和端口号 + diff --git a/works/OLDERHARD/M2.2/M2.2.txt b/works/OLDERHARD/M2.2/M2.2.txt new file mode 100644 index 0000000..da1ba09 --- /dev/null +++ b/works/OLDERHARD/M2.2/M2.2.txt @@ -0,0 +1,38 @@ +后端路由: +后端路由是由服务器处理的。当客户端(通常是浏览器)发出请求时,服务器根据请求的 URL 来找到相应的映射函数,然后执行该函数并将结果返回给客户端。 + +后端路由的流程: +浏览器发出请求。 +服务器监听到请求并解析 URL 路径,这里是Flask。 +根据服务器的路由配置,返回相应信息(可以是 HTML 文件、JSON 数据或图片)。 +浏览器根据数据包的 Content-Type 来解析数据。 + + +当浏览器访问网页时,服务器会返回一个包含 HTTP 状态码的信息头,用以响应浏览器的请求。这些状态码表示服务器对请求的处理结果。以下是常见的 HTTP 状态码: + +1xx(信息性状态码):表示接收的请求正在处理。 +100 Continue:服务器已接收到请求的头部,客户端可以继续发送请求的主体部分。 +101 Switching Protocols:服务器要求客户端切换协议。 + +2xx(成功状态码):表示请求正常处理完毕。 +200 OK:请求成功,一般用于 GET 和 POST 请求。 +201 Created:已创建,成功请求并创建了新的资源。 +204 No Content:无内容,服务器成功处理,但未返回内容。 + +3xx(重定向状态码):需要后续操作才能完成请求。 +300 Multiple Choices:请求的资源有多个选择,服务器可以根据请求的条件返回不同的资源。例如,浏览器请求一个目录时,服务器可以返回多个文件供用户选择。 +301 Moved Permanently:请求的 URL 已永久移动到新位置,用于网站重定向。 +302 Found:临时移动,资源只是临时被移动。 + +4xx(客户端错误状态码):表示请求包含语法错误或无法完成。 +400 Bad Request:客户端请求的语法错误,服务器无法理解。 +401 Unauthorized:表示客户端未经授权,需要提供有效的身份验证凭据。通常用于需要登录的资源。 +403 Forbidden:表示客户端没有访问权限,服务器拒绝请求。这可能是因为客户端没有登录、没有足够的权限或者其他原因。 +404 Not Found:服务器无法找到请求的资源,常见于访问不存在的页面。 + +5xx(服务器错误状态码):服务器在处理请求的过程中发生了错误。 +500 Internal Server Error:服务器内部错误,无法完成请求。 +501 Not Implemented:服务器不支持请求的功能,无法完成请求。这通常是因为服务器没有实现请求的方法(例如,某个 HTTP 方法)。 +502 Bad Gateway:表示服务器作为网关或代理,从上游服务器接收到无效的响应。通常用于反向代理服务器。 +503 Service Unavailable:服务器暂时无法处理客户端的请求。 +504 Gateway Timeout:表示服务器作为网关或代理,无法在规定的时间内从上游服务器接收到响应。这通常是因为上游服务器处理请求的时间过长。 diff --git a/works/OLDERHARD/M2.2/app.py b/works/OLDERHARD/M2.2/app.py new file mode 100644 index 0000000..581a2c6 --- /dev/null +++ b/works/OLDERHARD/M2.2/app.py @@ -0,0 +1,132 @@ +from flask import Flask,request,jsonify,Response,make_response +from fake_useragent import UserAgent +import requests +import json +from datetime import timedelta,datetime +import pytz + + +def solve1(name): + url="https://codeforces.com/api/user.info" + headers={ + 'User-Agent':UserAgent().random + } + params={ + "handles": name + } + try: + response=requests.get(url=url,params=params,headers=headers) + status_code=response.status_code + if status_code==200: # 此项 handle 可以查询到 + Json=json.loads(response.text) + user=Json['result'][0] + if 'rating' in user: + return { + 'success':True, + 'result':{ + 'handle':user['handle'], + 'rating':int(user['rating']), + 'rank':user['rank'] + } + } + else: + return { + 'success':True, + 'result':{ + 'handle':user['handle'] + } + + } + elif status_code==400: # 此项 handle 无法找到 + return { + 'success':True, + 'type':1, + 'message':'no such handle' + } + else: # 在查询此项时遭遇异常 HTTP 响应 + return { + 'success':False, + 'type':2, + 'message':'HTTP response with code {}'.format(status_code), + 'datails':{ + 'status':status_code + } + } + except requests.exceptions.ConnectionError or requests.exceptions.RequestException as e: # 在查询此项时未收到有效 HTTP 响应 + return { + 'success':False, + 'type':3, + 'message':'Request timeout' + } + except: # 在查询此项时程序发生运行时异常 + return { + 'success':False, + 'type':4, + 'message':'Internal Server Error' + } + + +def solve2(name): + url=f"https://codeforces.com/api/user.rating?handle={name}" + headers={ + 'User-Agent':UserAgent().random + } + + try: + response=requests.get(url=url,params=name,headers=headers) + status_code=response.status_code + ans=[] + if status_code==200: + Json=json.loads(response.text) + user=Json['result'] + for date in user: + ratingUpdateTime=date['ratingUpdateTimeSeconds'] + time=datetime.fromtimestamp(ratingUpdateTime, pytz.timezone('Asia/Shanghai')) + ratingUpdateAt=time.isoformat() + result={ + 'handle':date['handle'], + 'contestId':date['contestId'], + 'contestName':date['contestName'], + 'rank':int(date['rank']), + 'ratingUpdateAt':ratingUpdateAt, + 'oldRating':int(date['oldRating']), + 'newRating':int(date['newRating']) + } + ans.append(result) + return ans + elif status_code==400: + return { + 'message':'no such handle' + } + else: + return { + 'message':'HTTP response with code {}'.format(status_code) + } + except requests.exceptions.ConnectionError or requests.exceptions.RequestException as e: + return { + 'message': 'Request timeout', + } + except: + return { + 'message': 'Internal Server Error' + } + +app= Flask(__name__) +@app.route('/batchGetUserInfo') +def get1(): + handles=request.args.get('handles').split(',') + results=[] + for handle in handles: + result =solve1(handle) + results.append(result) + return Response(json.dumps(results),mimetype='application/json') + +@app.route('/getUserRatings') +def get2(): #单用户查询接口 + handle = request.args.get('handle') + result = [] + result = solve2(handle) + return Response(json.dumps(result), mimetype='application/json') + +if __name__ == '__main__': + app.run(host='127.0.0.1', port=2333, debug=True) diff --git a/works/OLDERHARD/M2.3/M2.3.txt b/works/OLDERHARD/M2.3/M2.3.txt new file mode 100644 index 0000000..4083da4 --- /dev/null +++ b/works/OLDERHARD/M2.3/M2.3.txt @@ -0,0 +1,16 @@ +缓存是一种存储机制,旨在提供高速访问已保存的数据或计算结果。它通过将数据存储在临时存储位置,当再次需要这些数据时,可以迅速从缓存中检索,而不是重新进行原始数据的昂贵或时间耗费的获取和计算过程。 + +在设计缓存系统时,需要考虑不同缓存策略的优缺点和适用场景,以优化缓存系统的性能和效率。以下是一些常见的缓存策略: + +Cache-Aside(旁路缓存): +应用程序首先检查缓存中是否有请求的数据,如果有则返回缓存的数据,否则从数据库查询数据并更新缓存,然后返回数据。 +Read-Through(读取穿透): +缓存对数据库进行读取/查询操作,然后更新自己并将请求数据返回给最终用户。 +Refresh-Ahead(预刷新): +在数据过期之前刷新缓存数据,适用于热数据。 +Write-Through(写穿透): +缓存作为主数据存储,首先在缓存中更新数据,然后才在数据库中更新数据。 +Write-Back(写回): +类似于 write-through,但数据库写调用是异步的。 + +当然,本次作业中,这些都没用到,就用了两个字典存储了已访问且未超时的数据一个用来存储访问 /batchGetUserInfo 路由的信息,另一个存储访问 /getUserRatings 路由的信息,提高检索速度 diff --git a/works/OLDERHARD/M2.3/app.py b/works/OLDERHARD/M2.3/app.py new file mode 100644 index 0000000..23d689e --- /dev/null +++ b/works/OLDERHARD/M2.3/app.py @@ -0,0 +1,148 @@ +from flask import Flask,request,jsonify,Response +from fake_useragent import UserAgent +import requests +import json +from datetime import timedelta,datetime +import pytz + +map_user={} +map_rating={} + +def solve1(name): + if name in map_user and map_user[name]['time']>datetime.now(): + return map_user[name]['date'] + + url='https://codeforces.com/api/user.info' + headers={ + 'User-Agent':UserAgent().random + } + params={ + 'handles':name + } + try: + response=requests.get(url=url,params=params,headers=headers) + status_code=response.status_code + if status_code==200: + Json=json.loads(response.text) + user=Json['result'][0] + if 'rating' in user: + date={ + 'success':True, + 'result':{ + 'handle':user['handle'], + 'rating':int(user['rating']), + 'rank':user['rank'] + } + } + else: + date={ + 'success':True, + 'result':{ + 'handle':user['handle'] + } + } + elif status_code==400: + date={ + 'success':True, + 'type':1, + 'message':'no such handle', + } + else: + date={ + 'success':False, + 'type':2, + 'message':'HTTP response with code {}'.format(status_code), + 'datails':{ + 'status':status_code + } + } + map_user[name]={ + 'date':date, + 'time':datetime.now()+timedelta(seconds=15) + } + return date + except requests.exceptions.ConnectionError or requests.exceptions.RequestException as e: + return { + 'success':False, + 'type':3, + 'message':'Request timeout' + } + except: + return { + 'success':False, + 'type':4, + 'message':'Internal Server Error' + } + +def solve2(name): + if name in map_rating and map_rating[name]['time']>datetime.now(): + return map_rating[name]['date'] + + url=f"https://codeforces.com/api/user.rating?handle={name}" + headers={ + 'User-Agent': UserAgent().random + } + params={ + 'handles': name + } + try: + response=requests.get(url=url,params=params,headers=headers) + status_code=response.status_code + if status_code==200: + Json=json.loads(response.text) + user=Json['result'] + ans=[] + for date in user: + ratingUpdateTime=date['ratingUpdateTimeSeconds'] + time=datetime.fromtimestamp(ratingUpdateTime,pytz.timezone('Asia/Shanghai')) + ratingUpdateAt=time.isoformat() + result={ + 'handle':date['handle'], + 'contestId':date['contestId'], + 'contestName':date['contestName'], + 'rank':int(date['rank']), + 'ratingUpdateAt':ratingUpdateAt, + 'oldRating':int(date['oldRating']), + 'newRating':int(date['newRating']) + } + ans.append(result) + elif status_code==400: + ans={ + 'message':'no such handle' + } + else: + ans={ + 'message':'HTTP response with code {}'.format(status_code) + } + map_rating[name]={ + 'date':ans, + 'time':datetime.now()+timedelta(seconds=15) + } + return ans + except requests.exceptions.ConnectionError or requests.exceptions.RequestException as e: + return { + 'message':'Request timeout' + } + except: + return { + 'message':'Internal Server Error' + } + + +app=Flask(__name__) +@app.route('/batchGetUserInfo') +def get1(): + handles=request.args.get('handles').split(',') + results=[] + for handle in handles: + result=solve1(handle) + results.append(result) + return Response(json.dumps(results),mimetype='application/json') +@app.route('/getUserRatings') +def get2(): + handle=request.args.get('handle') + result=[] + result=solve2(handle) + return Response(json.dumps(result),mimetype='application/json') +if __name__=='__main__': + app.run(host='127.0.0.1',port=2333,debug=True) diff --git a/works/OLDERHARD/M2.4/M2.4.txt b/works/OLDERHARD/M2.4/M2.4.txt new file mode 100644 index 0000000..ad3a275 --- /dev/null +++ b/works/OLDERHARD/M2.4/M2.4.txt @@ -0,0 +1,10 @@ +POST请求的几种Content-Type +1. application/x-www-form-urlencoded +“application/x-www-form-urlencoded”是POST请求方法中一种常见的Content-Type类型。对于浏览器的原生