目录
- HTTPServer
- 阻塞IO与非阻塞IO
- IO多路复用
- select方案
- 位运算
- poll方案
1. HTTPServer
#httpserver.py
#服务端 httpserver
from socket import *
#定义处理客户端请求函数
def handleClient(connfd):
request = connfd.recv(4096)
request_lines = request.splitlines()
#bytes.splitlines()
for line in request_lines:
print(line.decode())
try:
f = open("index.html")
except IOError :
response = "HTTP/1.1 404 not found\r\n"
response += "\r\n" #空行
response += "====Sorry not found===="
else: #未发生异常执行Else
response = "HTTP/1.1 200 OK\r\n"
response += "\r\n" #空行
response += f.read()
finally:
connfd.send(response.encode())
def main():
sockfd = socket()
sockfd.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
sockfd.bind(("0.0.0.0",8888))
sockfd.listen(3)
print("listen to the port 8888")
while True:
connfd, addr = sockfd.accept()
#调用处理客户端请求函数,并返回响应
handleClient(connfd)
connfd.close()
if __name__ == "__main__":
main()
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chrom</title>
</head>
<body>
<h1>My HTML</h1>
</body>
</html>
2. 阻塞IO与非阻塞IO
3. IO多路复用
4. select方案
5. 位运算
6. poll方案