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())
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)
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,))
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,))
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,))
转载请注明原文地址:https://ipadbbs.8miu.com/read-18810.html