还是之前编写的程序,今天开始用notepad++调用之前的程序运行了一下,居然发现运行结果不一样。为。。。。。撒?
以下是代码:
import socket
import re
import multiprocessing
def service_client(client_socket):
data = client_socket.recv(1024)
recv_data=data.decode("utf-8")
print(recv_data)
#GET /index.html HTTP/1.1
ret=re.match(r"[^/]+ /([\S]+\s|\s)",recv_data)
file_n="index.html"
if ret: #判断正则筛取的结果
result=ret.group(1).strip() # 开始我想是不是因为文件名有空格,所以说找不到,后来发现即便是这样改了后面的结果还是一样。???
if result!="":
print(result)
file_n=result
else:
print("代码有误!")
try:
with open(file_n,"rb") as f:
content=f.read()
f.close()
except Exception as r:
print(r)
content = "file not found<\h1>"
.encode("utf-8")
header="HTTP/1.1 200 OK\r\n"
empty="\r\n"
response=(header+empty).encode('utf-8')
client_socket.send(response)
client_socket.send(content)
#print(content)
client_socket.close()
print("client_socket.close()已调用")
def main():
tcp_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
local_addr=("127.0.0.1",7890)
tcp_socket.bind(local_addr)
tcp_socket.listen(128)
while True:
client_socket, client_addr = tcp_socket.accept()
p=multiprocessing.Process(target=service_client,args=(client_socket,))
p.start()
# p.join()
tcp_socket.close()
if __name__ == "__main__":
main()
以下是在pycharm中的运行结果:
"D:\new file\python-3.6.4 install\python.exe" "D:/new file/正则和http协议/正则和http协议2.py"
GET / HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
client_socket.close()已调用
GET /css/main.css HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/css,*/*;q=0.1
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
css/main.css
client_socket.close()已调用
GET /css/reset.css HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: text/css,*/*;q=0.1
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
css/reset.css
client_socket.close()已调用
GET /images/logo.png HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
images/logo.png
client_socket.close()已调用
GET /images/slide.jpg HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
images/slide.jpg
client_socket.close()已调用
GET /images/adv01.jpg HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
images/adv01.jpg
client_socket.close()已调用
GET /images/adv02.jpg HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
images/adv02.jpg
client_socket.close()已调用
GET /images/goods.jpg HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
images/goods.jpg
GET /images/icons.png HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/css/main.css
Cache-Control: max-age=0
GET /images/banner01.jpg HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
images/icons.png
images/banner01.jpg
GET /images/banner02.jpg HTTP/1.1
Host: 127.0.0.1:7890
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:69.0) Gecko/20100101 Firefox/69.0
Accept: image/webp,*/*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1:7890/
Cache-Control: max-age=0
images/banner02.jpg
client_socket.close()已调用
client_socket.close()已调用
client_socket.close()已调用
client_socket.close()已调用
进程已结束,退出代码1
下面是在notepad++中的运行结果: