Python笔记——socket2

    技术2022-07-11  72

    #1、通过soket完成一个页面显示 import socket body = "Hello world" string = f"HTTP/1.1 200 ok\r\nContent-Length:{len(body)}\r\n\r\n{body}" service = socket.socket() service.bind(("127.0.0.1",23333)) service.listen(10) client , IP = service.accept() client.send(string.encode()) #2、通过Soket返回一个图片 import socket fail = open(r"D:\学习\python\python高阶第02天网络原理\滑稽.jpg","rb").read() body = fail string = f"HTTP/1.1 200 ok\r\nContent-Length:{len(body)}\r\nSet-Cookie:login=true\r\n\r\n" service = socket.socket() service.bind(("127.0.0.1",23333)) service.listen(10) client , IP = service.accept() client.send(string.encode()+body) #3、通过浏览器地址,访问不同的页面 import socket import _thread path = r"D:\学习\python\python高阶第02天网络原理\下午HTTP协议" service = socket.socket() service.bind(("127.0.0.1", 23333)) service.listen(10) def run(client: socket.socket): s = client.recv(1024 * 1024) a = str(s.split("\r\n\r\n".encode())[0]) head = a.split("\r\n") l = head[0].split(" ") l[1] = l[1].replace("/", r"\\") body = open(path + l[1], "rb").read() string = f"HTTP/1.1 200 ok\r\nContent-Length:{len(body)}\r\n\r\n" client.send(string.encode() + body) client.close() while True: client, ip = service.accept() _thread.start_new_thread(run, (client,)) #4、写出一个登陆页面,实现一个登陆功能 import socket import _thread path = r"D:\学习\python\python高阶第02天网络原理\下午HTTP协议" service = socket.socket() service.bind(("127.0.0.1", 23333)) service.listen(10) def login(d: dict): name = d["name"] password = d["password"] if name == "admin" and password == "admin": return "true" else: return "false" def run(client: socket.socket): s = client.recv(1024 * 5) a = s.split("\r\n\r\n".encode()) d = {} if len(a) > 1 and len(a[1]) > 0: a[1] = a[1].decode() lists = a[1].split("&") for i in lists: b = i.split("=") d[b[0]] = b[1] a = str(a[0]) head = a.split("\r\n") l = head[0].split(" ") l1 = l[1].split("?") if len(l1) > 1: lists = l1[1].split("&") for i in lists: a = i.split("=") d[a[0]] = a[1] if l1[0] == "/login": c = login(d) body = c.encode() else: body = open(path + l1[0], "rb").read() string = f"HTTP/1.1 200 ok\r\nContent-Length:{len(body)}\r\nSet-Cookie:login=true\r\n\r\n" client.send(string.encode() + body) client.close() while True: client, ip = service.accept() _thread.start_new_thread(run, (client,)) #5、在登陆功能的基础上,如果用户已经登陆,则返回已经登陆信息(cookie) import socket import _thread path = r"D:\学习\python\python高阶第02天网络原理\下午HTTP协议" service = socket.socket() service.bind(("127.0.0.1", 23333)) service.listen(10) def login(d: dict): name = d["name"] password = d["password"] if name == "admin" and password == "admin": return "true" else: return "false" def run(client: socket.socket): s = client.recv(1024 * 5) a = s.split("\r\n\r\n".encode()) print(a) d = {} if len(a) > 1 and len(a[1]) > 0: a[1] = a[1].decode() print(a[1]) lists = a[1].split("&") print(lists) for i in lists: b = i.split("=") print(b) d[b[0]] = b[1] a = str(a[0]) head = a.split("\r\n") l_I = head[0].split("Cookie:") print(f"登录信息:{l_I[1]}") l = head[0].split(" ") l1 = l[1].split("?") if len(l1) > 1: lists = l1[1].split("&") for i in lists: a = i.split("=") d[a[0]] = a[1] if l1[0] == "/login": c = login(d) body = c.encode() else: body = open(path + l1[0], "rb").read() string = f"HTTP/1.1 200 ok\r\nContent-Length:{len(body)}\r\nSet-Cookie:login=true\r\n\r\n" client.send(string.encode() + body) client.close() while True: client, ip = service.accept() _thread.start_new_thread(run, (client,))
    Processed: 0.010, SQL: 9