http_server

来源:4-12 正确认识http协议 -2

weixin_慕斯卡4281563

2020-05-07

import socket
import threading
server = socket.socket()
server.bind(('0.0.0.0', 3000))
server.listen()


def handle_sock(sock, addr):
    while True:
        # recv方法是阻塞的,会一直从缓存里取数据,如果缓存里没有数据,会一直阻塞在这里
        tmp_data = sock.recv(1024)
        print("client response: {}".format(tmp_data.decode()))
        response_template = '''HTTP/1.1 200 OK

<html>
  <head>
    <title>Build A Web Server</title>
  </head>
  <body>
    Hello World, this is a very simple HTML document.
  </body>
</html>

'''
        sock.send(response_template.encode())


# 获取客户端连接并启动线程去处理
while True:
    # 阻塞等待连接
    sock, addr = server.accept()
    print(addr)
    # sock.send(str(addr[1]).encode() + "  welcome to server!".encode())
    print('连接成功')
    # 启动一个线程去处理新的用户连接
    client_thread = threading.Thread(target=handle_sock, args=(sock, addr))
    client_thread.start()
    print('线程创建成功')

我用上面的代码,然后在chrome浏览器端输入127.0.0.1:3000可以看到页面(Hello World, this is a very simple HTML document.)
在pycharm中run窗口的显示如下:

C:\Users\u243216\AppData\Local\Programs\Python\Python37\python.exe C:/Users/u243216/Desktop/spider/socket_test/http_server.py
('127.0.0.1', 57868)
连接成功
线程创建成功
('127.0.0.1', 57869)
连接成功
线程创建成功
client response: GET / HTTP/1.1
Host: 127.0.0.1:3000
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Purpose: prefetch
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6
Cookie: _ga=GA1.1.539617129.1580385658; UM_distinctid=16ff653853715e-0ae2be03f7741e-b383f66-e1000-16ff65385382d; CNZZDATA1254020586=56594503-1580385662-http%253A%252F%252F127.0.0.1%253A3000%252F%7C1581474641


client response: 
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\u243216\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\u243216\AppData\Local\Programs\Python\Python37\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/u243216/Desktop/spider/socket_test/http_server.py", line 11, in handle_sock
    tmp_data = sock.recv(1024)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

我的问题是:

  1. 观察到主线程(程序)中创建了两个连接(‘127.0.0.1’, 57868)和(‘127.0.0.1’, 57869), 也创建了两个线程去执行handle_sock方法。是不是因为 (‘127.0.0.1’, 57868)这个连接所创建的线程中handle_sock方法与浏览器交互比较慢才导致主程序又接受了浏览器的再次连接而创建了第二个连接(‘127.0.0.1’, 57869)?如果我的猜测是对的,是不是可以理解成浏览器里输入http://127.0.0.1:3000/ 回车后发起了不止一次连接server的请求?这是为什么呢?浏览器为什么要发起二次连接请求到服务器?
  2. 最后报‘ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine’ 应该是因为(‘127.0.0.1’, 57869)这个连接所创建的线程中tmp_data = sock.recv(1024)迟迟收不到浏览器返回的http报文而报的错,对不对呢?
写回答

1回答

bobby

2020-05-09

你可以把两个源码都贴一下 我在本地看看是否能重现这个问题

0
2
bobby
回复
weixin_慕斯卡4281563
https://www.cnblogs.com/zhaofeis/p/12811337.html 这里可以不加载favicon,视频我回头看看
2020-05-11
共2条回复

Python爬虫工程师实战 大数据时代必备

慕课网严选精品教程,高质量内容+服务!

2381 学习 · 1158 问题

查看课程