源于知乎上一个问题:https://www.zhihu.com/question/56472691/answer/293292349
对于初学Web开发,理解一个web server如何能同事处理多个请求很重要。当然更重要的是,理解你通过浏览器发送的请求web server是怎么处理的,然后怎么返回给浏览器,浏览器才能展示的。
我到现在还记得大概在2010年左右,看了tomcat源码之后,那种豁然开朗的感觉(对,当时我还在写Java)。搞技术的人,总是希望花点时间,能够更透彻的看透整个网络世界的构成,而不是那啥。
要理解web server如何能处理多个请求有两个基本要素
通过这两段代码,应该很好理解:
# coding:utf-8
import socket
EOL1 = '\n\n'
EOL2 = '\n\r\n'
body = '''Hello, world! <h1> from the5fire 《Django企业开发实战》</h1>'''
response_params = [ 'HTTP/1.0 200 OK', 'Date: Sat, 10 jun 2017 01:01:01 GMT', 'Content-Type: text/plain; charset=utf-8', 'Content-Length: {}\r\n'.format(len(body)), body, ]
response = b'\r\n'.join(response_params)
def handle_connection(conn, addr):
request = ""
while EOL1 not in request and EOL2 not in request:
request += conn.recv(1024)
print(request)
conn.send(response)
conn.close()
def main():
# socket.AF_INET 用于服务器与服务器之间的网络通信
# socket.SOCK_STREAM 基于TCP的流式socket通信
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 设置端口可复用,保证我们每次Ctrl C之后,快速再次重启
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(('127.0.0.1', 8080))
# 可参考:https://stackoverflow.com/questions/2444459/python-sock-listen
serversocket.listen(1)
print('http://127.0.0.1:8080')
try:
while True:
conn, address = serversocket.accept()
handle_connection(conn, address)
finally:
serversocket.close()
if __name__ == '__main__':
main()
python server.py
试试
# coding:utf-8
import socket
import threading
import time
EOL1 = '\n\n'
EOL2 = '\n\r\n'
body = '''Hello, world! <h1> from the5fire 《Django企业开发实战》</h1> - from {thread_name}'''
response_params = [ 'HTTP/1.0 200 OK', 'Date: Sat, 10 jun 2017 01:01:01 GMT', 'Content-Type: text/plain; charset=utf-8', 'Content-Length: {length}\r\n', body, ]
response = b'\r\n'.join(response_params)
def handle_connection(conn, addr):
request = ""
while EOL1 not in request and EOL2 not in request:
request += conn.recv(1024)
# print(request)
current_thread = threading.currentThread()
content_length = len(body.format(thread_name=current_thread.name))
print(current_thread.name, '-------', 'sleep 10', int(time.time()))
time.sleep(10)
conn.send(response.format(thread_name=current_thread.name, length=content_length))
conn.close()
def main():
# socket.AF_INET 用于服务器与服务器之间的网络通信
# socket.SOCK_STREAM 基于TCP的流式socket通信
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 设置端口可复用,保证我们每次Ctrl C之后,快速再次重启
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(('127.0.0.1', 8080))
# 可参考:https://stackoverflow.com/questions/2444459/python-sock-listen
serversocket.listen(10)
print('http://127.0.0.1:8080')
try:
i = 0
while True:
i += 1
conn, address = serversocket.accept()
t = threading.Thread(target=handle_connection, args=(conn, address), name='thread-%s' % i)
t.start()
finally:
serversocket.close()
if __name__ == '__main__': main()
试试吧,与其着急去学习框架不如先弄懂这个。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有