From c6e74656c4522f519746bee4f58f24f4db98baeb Mon Sep 17 00:00:00 2001 From: Thinkless Date: Fri, 25 Nov 2016 21:22:12 +0800 Subject: [PATCH 01/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 79c8b30..031b4ef 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Python_Web_study 笔记 +#Python_Web_study 笔记 ##1.log大法 ```python def log(*args, **kwargs): From 79cceb9d5fa9f5402fd35fa283ebf2beaead3118 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Wed, 30 Nov 2016 21:34:31 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E8=AF=BE=E5=89=8D=E4=B8=A4=E9=83=A8=E5=88=86=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class2/class2_1_2_code | 185 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 class2/class2_1_2_code diff --git a/class2/class2_1_2_code b/class2/class2_1_2_code new file mode 100644 index 0000000..f5f45e6 --- /dev/null +++ b/class2/class2_1_2_code @@ -0,0 +1,185 @@ +# coding: utf-8 +""" +课 2 上课用品 +2016.8.11 + +本次上课的主要内容有 +0, 请注意代码的格式和规范 +1, 规范化生成响应 +2, HTTP 头 +3, 几个常用 HTML 标签及其用法 +4, HTTP 参数传递的两种方式 + +""" +# 下面这行注释是给 atom 的 pylint 用的, 忽略 +# pylint: disable=C0103 + +""" +url 的规范 +第一个 ? 之前的是 path +? 之后的是 query +http://c.cc/search?a=b&c=d&e=1 +PATH /search +QUERY a=b&c=d&e=1 +""" +import socket + + +# 定义一个 class 用于保存请求的数据 +class Request(object): + def __init__(self): + self.path = '' + self.query = {} + +# 定义一个 class 用于保存 message +class Message(object): + def __init__(self): + self.message = '' + self.author = '' + + def __repr__(self): + return '{}: {}'.format(self.author, self.message) +# +message_list = [] +request = Request() + + +def log(*args, **kwargs): + """ + 用这个 log 替代 print + """ + print('log', *args, **kwargs) + + +def template(name): + with open(name, 'r', encoding='utf-8') as f: + return f.read() + + +def route_index(): + """ + 主页的处理函数, 返回主页的响应 + """ + header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' + body = '

Hello World

' + r = header + '\r\n' + body + return r.encode(encoding='utf-8') + + +def route_message(): + """ + 主页的处理函数, 返回主页的响应 + """ + msg = Message() + msg.author = request.query.get('author', '') + msg.message = request.query.get('message', '') + message_list.append(msg) + header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' + # body = '

消息版

' + body = template('html_basic.html') + msgs = '
'.join([str(m) for m in message_list]) + body = body.replace('{{messages}}', msgs) + r = header + '\r\n' + body + return r.encode(encoding='utf-8') + + +def route_image(): + """ + 图片的处理函数, 读取图片并生成响应返回 + """ + with open('doge.gif', 'rb') as f: + header = b'HTTP/1.x 200 OK\r\nContent-Type: image/gif\r\n\r\n' + img = header + f.read() + return img + + +def error(code=404): + """ + 根据 code 返回不同的错误响应 + 目前只有 404 + """ + # 之前上课我说过不要用数字来作为字典的 key + # 但是在 HTTP 协议中 code 都是数字似乎更方便所以打破了这个原则 + e = { + 404: b'HTTP/1.x 404 NOT FOUND\r\n\r\n

NOT FOUND

', + } + return e.get(code, b'') + + +def parsed_path(path): + """ + message=hello&author=gua + { + 'message': 'hello', + 'author': 'gua', + } + """ + index = path.find('?') + if index == -1: + return path, {} + else: + path, query_string = path.split('?', 1) + args = query_string.split('&') + query = {} + for arg in args: + k, v = arg.split('=') + query[k] = v + return path, query + + +def response_for_path(path): + path, query = parsed_path(path) + request.path = path + request.query = query + log('path and query', path, query) + """ + 根据 path 调用相应的处理函数 + 没有处理的 path 会返回 404 + """ + r = { + '/': route_index, + '/doge.gif': route_image, + '/messages': route_message, + } + response = r.get(path, error) + return response() + + +def run(host='', port=3000): + """ + 启动服务器 + """ + # 初始化 socket 套路 + # 使用 with 可以保证程序中断的时候正确关闭 socket 释放占用的端口 + with socket.socket() as s: + s.bind((host, port)) + # 无限循环来处理请求 + while True: + # 监听 接受 读取请求数据 解码成字符串 + s.listen(3) + connection, address = s.accept() + request = connection.recv(1000) + request = request.decode('utf-8') + log('ip and request, {}\n{}'.format(address, request)) + try: + # 因为 chrome 会发送空请求导致 split 得到空 list + # 所以这里用 try 防止程序崩溃 + path = request.split()[1] + # 用 response_for_path 函数来得到 path 对应的响应内容 + response = response_for_path(path) + # 把响应发送给客户端 + connection.sendall(response) + except Exception as e: + log('error', e) + # 处理完请求, 关闭连接 + connection.close() + + +if __name__ == '__main__': + # 生成配置并且运行程序 + config = dict( + host='', + port=3000, + ) + # 如果不了解 **kwargs 的用法, 上过基础课的请复习函数, 新同学自行搜索 + run(**config) From 8bda836d6a04cb799ebbead5df9b91098c6fadd1 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Wed, 30 Nov 2016 21:34:59 +0800 Subject: [PATCH 03/13] Delete class2_1_2_code --- class2/class2_1_2_code | 185 ----------------------------------------- 1 file changed, 185 deletions(-) delete mode 100644 class2/class2_1_2_code diff --git a/class2/class2_1_2_code b/class2/class2_1_2_code deleted file mode 100644 index f5f45e6..0000000 --- a/class2/class2_1_2_code +++ /dev/null @@ -1,185 +0,0 @@ -# coding: utf-8 -""" -课 2 上课用品 -2016.8.11 - -本次上课的主要内容有 -0, 请注意代码的格式和规范 -1, 规范化生成响应 -2, HTTP 头 -3, 几个常用 HTML 标签及其用法 -4, HTTP 参数传递的两种方式 - -""" -# 下面这行注释是给 atom 的 pylint 用的, 忽略 -# pylint: disable=C0103 - -""" -url 的规范 -第一个 ? 之前的是 path -? 之后的是 query -http://c.cc/search?a=b&c=d&e=1 -PATH /search -QUERY a=b&c=d&e=1 -""" -import socket - - -# 定义一个 class 用于保存请求的数据 -class Request(object): - def __init__(self): - self.path = '' - self.query = {} - -# 定义一个 class 用于保存 message -class Message(object): - def __init__(self): - self.message = '' - self.author = '' - - def __repr__(self): - return '{}: {}'.format(self.author, self.message) -# -message_list = [] -request = Request() - - -def log(*args, **kwargs): - """ - 用这个 log 替代 print - """ - print('log', *args, **kwargs) - - -def template(name): - with open(name, 'r', encoding='utf-8') as f: - return f.read() - - -def route_index(): - """ - 主页的处理函数, 返回主页的响应 - """ - header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' - body = '

Hello World

' - r = header + '\r\n' + body - return r.encode(encoding='utf-8') - - -def route_message(): - """ - 主页的处理函数, 返回主页的响应 - """ - msg = Message() - msg.author = request.query.get('author', '') - msg.message = request.query.get('message', '') - message_list.append(msg) - header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' - # body = '

消息版

' - body = template('html_basic.html') - msgs = '
'.join([str(m) for m in message_list]) - body = body.replace('{{messages}}', msgs) - r = header + '\r\n' + body - return r.encode(encoding='utf-8') - - -def route_image(): - """ - 图片的处理函数, 读取图片并生成响应返回 - """ - with open('doge.gif', 'rb') as f: - header = b'HTTP/1.x 200 OK\r\nContent-Type: image/gif\r\n\r\n' - img = header + f.read() - return img - - -def error(code=404): - """ - 根据 code 返回不同的错误响应 - 目前只有 404 - """ - # 之前上课我说过不要用数字来作为字典的 key - # 但是在 HTTP 协议中 code 都是数字似乎更方便所以打破了这个原则 - e = { - 404: b'HTTP/1.x 404 NOT FOUND\r\n\r\n

NOT FOUND

', - } - return e.get(code, b'') - - -def parsed_path(path): - """ - message=hello&author=gua - { - 'message': 'hello', - 'author': 'gua', - } - """ - index = path.find('?') - if index == -1: - return path, {} - else: - path, query_string = path.split('?', 1) - args = query_string.split('&') - query = {} - for arg in args: - k, v = arg.split('=') - query[k] = v - return path, query - - -def response_for_path(path): - path, query = parsed_path(path) - request.path = path - request.query = query - log('path and query', path, query) - """ - 根据 path 调用相应的处理函数 - 没有处理的 path 会返回 404 - """ - r = { - '/': route_index, - '/doge.gif': route_image, - '/messages': route_message, - } - response = r.get(path, error) - return response() - - -def run(host='', port=3000): - """ - 启动服务器 - """ - # 初始化 socket 套路 - # 使用 with 可以保证程序中断的时候正确关闭 socket 释放占用的端口 - with socket.socket() as s: - s.bind((host, port)) - # 无限循环来处理请求 - while True: - # 监听 接受 读取请求数据 解码成字符串 - s.listen(3) - connection, address = s.accept() - request = connection.recv(1000) - request = request.decode('utf-8') - log('ip and request, {}\n{}'.format(address, request)) - try: - # 因为 chrome 会发送空请求导致 split 得到空 list - # 所以这里用 try 防止程序崩溃 - path = request.split()[1] - # 用 response_for_path 函数来得到 path 对应的响应内容 - response = response_for_path(path) - # 把响应发送给客户端 - connection.sendall(response) - except Exception as e: - log('error', e) - # 处理完请求, 关闭连接 - connection.close() - - -if __name__ == '__main__': - # 生成配置并且运行程序 - config = dict( - host='', - port=3000, - ) - # 如果不了解 **kwargs 的用法, 上过基础课的请复习函数, 新同学自行搜索 - run(**config) From f0469cc63bcd2beef038a039b968635080994148 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Wed, 30 Nov 2016 21:36:20 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E8=AF=BE=E5=89=8D=E4=B8=A4=E5=B0=8F=E6=AE=B5=E8=AF=BE=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- class2/class2_1_2_code.py | 185 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 class2/class2_1_2_code.py diff --git a/class2/class2_1_2_code.py b/class2/class2_1_2_code.py new file mode 100644 index 0000000..f5f45e6 --- /dev/null +++ b/class2/class2_1_2_code.py @@ -0,0 +1,185 @@ +# coding: utf-8 +""" +课 2 上课用品 +2016.8.11 + +本次上课的主要内容有 +0, 请注意代码的格式和规范 +1, 规范化生成响应 +2, HTTP 头 +3, 几个常用 HTML 标签及其用法 +4, HTTP 参数传递的两种方式 + +""" +# 下面这行注释是给 atom 的 pylint 用的, 忽略 +# pylint: disable=C0103 + +""" +url 的规范 +第一个 ? 之前的是 path +? 之后的是 query +http://c.cc/search?a=b&c=d&e=1 +PATH /search +QUERY a=b&c=d&e=1 +""" +import socket + + +# 定义一个 class 用于保存请求的数据 +class Request(object): + def __init__(self): + self.path = '' + self.query = {} + +# 定义一个 class 用于保存 message +class Message(object): + def __init__(self): + self.message = '' + self.author = '' + + def __repr__(self): + return '{}: {}'.format(self.author, self.message) +# +message_list = [] +request = Request() + + +def log(*args, **kwargs): + """ + 用这个 log 替代 print + """ + print('log', *args, **kwargs) + + +def template(name): + with open(name, 'r', encoding='utf-8') as f: + return f.read() + + +def route_index(): + """ + 主页的处理函数, 返回主页的响应 + """ + header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' + body = '

Hello World

' + r = header + '\r\n' + body + return r.encode(encoding='utf-8') + + +def route_message(): + """ + 主页的处理函数, 返回主页的响应 + """ + msg = Message() + msg.author = request.query.get('author', '') + msg.message = request.query.get('message', '') + message_list.append(msg) + header = 'HTTP/1.x 210 VERY OK\r\nContent-Type: text/html\r\n' + # body = '

消息版

' + body = template('html_basic.html') + msgs = '
'.join([str(m) for m in message_list]) + body = body.replace('{{messages}}', msgs) + r = header + '\r\n' + body + return r.encode(encoding='utf-8') + + +def route_image(): + """ + 图片的处理函数, 读取图片并生成响应返回 + """ + with open('doge.gif', 'rb') as f: + header = b'HTTP/1.x 200 OK\r\nContent-Type: image/gif\r\n\r\n' + img = header + f.read() + return img + + +def error(code=404): + """ + 根据 code 返回不同的错误响应 + 目前只有 404 + """ + # 之前上课我说过不要用数字来作为字典的 key + # 但是在 HTTP 协议中 code 都是数字似乎更方便所以打破了这个原则 + e = { + 404: b'HTTP/1.x 404 NOT FOUND\r\n\r\n

NOT FOUND

', + } + return e.get(code, b'') + + +def parsed_path(path): + """ + message=hello&author=gua + { + 'message': 'hello', + 'author': 'gua', + } + """ + index = path.find('?') + if index == -1: + return path, {} + else: + path, query_string = path.split('?', 1) + args = query_string.split('&') + query = {} + for arg in args: + k, v = arg.split('=') + query[k] = v + return path, query + + +def response_for_path(path): + path, query = parsed_path(path) + request.path = path + request.query = query + log('path and query', path, query) + """ + 根据 path 调用相应的处理函数 + 没有处理的 path 会返回 404 + """ + r = { + '/': route_index, + '/doge.gif': route_image, + '/messages': route_message, + } + response = r.get(path, error) + return response() + + +def run(host='', port=3000): + """ + 启动服务器 + """ + # 初始化 socket 套路 + # 使用 with 可以保证程序中断的时候正确关闭 socket 释放占用的端口 + with socket.socket() as s: + s.bind((host, port)) + # 无限循环来处理请求 + while True: + # 监听 接受 读取请求数据 解码成字符串 + s.listen(3) + connection, address = s.accept() + request = connection.recv(1000) + request = request.decode('utf-8') + log('ip and request, {}\n{}'.format(address, request)) + try: + # 因为 chrome 会发送空请求导致 split 得到空 list + # 所以这里用 try 防止程序崩溃 + path = request.split()[1] + # 用 response_for_path 函数来得到 path 对应的响应内容 + response = response_for_path(path) + # 把响应发送给客户端 + connection.sendall(response) + except Exception as e: + log('error', e) + # 处理完请求, 关闭连接 + connection.close() + + +if __name__ == '__main__': + # 生成配置并且运行程序 + config = dict( + host='', + port=3000, + ) + # 如果不了解 **kwargs 的用法, 上过基础课的请复习函数, 新同学自行搜索 + run(**config) From 8aa433a9fe622755fe9219f5316541de8bc51832 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Thu, 8 Dec 2016 11:16:11 +0800 Subject: [PATCH 05/13] python_codecademy --- Python | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Python diff --git a/Python b/Python new file mode 100644 index 0000000..c827f70 --- /dev/null +++ b/Python @@ -0,0 +1,51 @@ +# 1.Python Syntax +*you can use it to create web apps, games, even a search engine* +*** +1. Variables + 1. numbers + 2. boolean + * True + * False +2. Whitespace *use to structure code* +3. Interpreter +4. Comments + 1. Singe line comments + ```python + #This is singe line comments + ``` + 2. Multi_line comments + ```python + """ + This is multi-line comments + """ + ``` +5. Arithmetic operations + 1. 算术 + ```python + addition = 72 + 23 + subtraction = 72 - 23 + multiplication = 72 * 23 + division = 72 / 23 + print addition, subtraction, multiplication, division + ``` + 2. Exponentiation + ```python + #the result of 2 to the power to 3 + eight = 2 ** 3 + ``` + 3. Modulo + ```python + spam = 13 % 4 + ``` +# 2.Strings & Console Output +* String methods +```python +""" +Methods that use dot notation only work with strings +On the other hand len() and str() can work on other data types +""" +len('python') +'python'.lower() +'python'.upper() +str('python') +``` From 763161c060004afe373ccec7ae49bef8290a4b60 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Thu, 8 Dec 2016 11:16:44 +0800 Subject: [PATCH 06/13] Rename Python to Python.md --- Python => Python.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Python => Python.md (100%) diff --git a/Python b/Python.md similarity index 100% rename from Python rename to Python.md From 4956633952947b82bd03e319bdc8cabc6cbdd3bc Mon Sep 17 00:00:00 2001 From: Thinkless Date: Thu, 8 Dec 2016 11:22:21 +0800 Subject: [PATCH 07/13] Delete Python.md --- Python.md | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 Python.md diff --git a/Python.md b/Python.md deleted file mode 100644 index c827f70..0000000 --- a/Python.md +++ /dev/null @@ -1,51 +0,0 @@ -# 1.Python Syntax -*you can use it to create web apps, games, even a search engine* -*** -1. Variables - 1. numbers - 2. boolean - * True - * False -2. Whitespace *use to structure code* -3. Interpreter -4. Comments - 1. Singe line comments - ```python - #This is singe line comments - ``` - 2. Multi_line comments - ```python - """ - This is multi-line comments - """ - ``` -5. Arithmetic operations - 1. 算术 - ```python - addition = 72 + 23 - subtraction = 72 - 23 - multiplication = 72 * 23 - division = 72 / 23 - print addition, subtraction, multiplication, division - ``` - 2. Exponentiation - ```python - #the result of 2 to the power to 3 - eight = 2 ** 3 - ``` - 3. Modulo - ```python - spam = 13 % 4 - ``` -# 2.Strings & Console Output -* String methods -```python -""" -Methods that use dot notation only work with strings -On the other hand len() and str() can work on other data types -""" -len('python') -'python'.lower() -'python'.upper() -str('python') -``` From 827b19c41f68cd4ef7fdc4a71e8a68a57961e9f0 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Tue, 19 Mar 2019 16:09:16 +0800 Subject: [PATCH 08/13] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 031b4ef..877768d 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ -#Python_Web_study 笔记 -##1.log大法 +# Python_Web_study 笔记 +## 1.log大法 ```python def log(*args, **kwargs): print('log', *args, **kwargs) ``` -##2.request格式 +## 2.request格式 ```python request = 'GET {} HTTP/1.1\r\nhost:{}\r\nCollection:close\r\n\r\n'.format(path, host) ``` -##3.server 接收 request, 并根据path 返回 response +## 3.server 接收 request, 并根据path 返回 response ```python #使用with可以保证在程序中断的时候正确关闭socket并释放占用的端口 with socket.socket() as s: @@ -27,7 +27,7 @@ with socket.socket() as s: log('error', e) connection.close() ``` -##4.response格式 +## 4.response格式 ```python def error(code=404): e = { @@ -62,7 +62,7 @@ def response_for_path(path): response = r.get(path, error) return response() ``` -##4.https +## 4.https ```python import ssl s = ssl.wrap_socket(socket.socket()) From a6d9413cf1e878d34b0a4b913f84b1a65deacbbc Mon Sep 17 00:00:00 2001 From: Xinhua Date: Wed, 3 Jun 2020 16:30:36 +0800 Subject: [PATCH 09/13] test --- class2/test.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 class2/test.md diff --git a/class2/test.md b/class2/test.md new file mode 100644 index 0000000..a13d669 --- /dev/null +++ b/class2/test.md @@ -0,0 +1,2 @@ +# test +1 \ No newline at end of file From 223331563e4e8cd41456aaf03a3fbecf3d28ed61 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Tue, 24 Nov 2020 17:58:10 +0800 Subject: [PATCH 10/13] save --- .vscode/settings.json | 3 +++ class2/test.md | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 class2/test.md diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4f95ced --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Program Files (x86)\\python.exe" +} \ No newline at end of file diff --git a/class2/test.md b/class2/test.md deleted file mode 100644 index a13d669..0000000 --- a/class2/test.md +++ /dev/null @@ -1,2 +0,0 @@ -# test -1 \ No newline at end of file From cb5d4dc616b0796ff40b29642a386b44c445fa68 Mon Sep 17 00:00:00 2001 From: Thinkless Date: Tue, 24 Nov 2020 17:58:28 +0800 Subject: [PATCH 11/13] save --- class1/git | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 class1/git diff --git a/class1/git b/class1/git new file mode 100644 index 0000000..e69de29 From 2242b6c95ba7f929b6381532966825692061ef16 Mon Sep 17 00:00:00 2001 From: Xinhua Date: Tue, 31 Aug 2021 14:31:29 +0800 Subject: [PATCH 12/13] test --- pfe/helloworld.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 pfe/helloworld.py diff --git a/pfe/helloworld.py b/pfe/helloworld.py new file mode 100644 index 0000000..7305ade --- /dev/null +++ b/pfe/helloworld.py @@ -0,0 +1,4 @@ +import typing + + +print('Hello World!') \ No newline at end of file From fc945504de8f0030dd7663eed8ac53a922bdd6b3 Mon Sep 17 00:00:00 2001 From: Xinhua Cheng Date: Thu, 21 Apr 2022 16:42:50 +0800 Subject: [PATCH 13/13] add test file --- test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e69de29