[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
今天尝试着仅使用单进程、单线程、不用协程就完成多任务的web服务器简单编程。问题多多,收获多多。首先放上核心代码
整体思路是没有问题的但是其间出现了很多小问题,这里就想记录一下:
1.程序一开始执行的时候,连接成功,但是无论怎么发数据,都接收不到
检查发现是代码中,由于一开始思路设计没有注意细节,.recv()中,没有设置参数。这是会产生异常的,导致收到数据和没有收到一样的结果—产生异常。
解决:通过打印异常e,得到如下结果,发现bug
通过这次粗心呢,其实也学到很多,首先使用time.sleep(),可以放慢过程,更好的测试程序;其次,是学会调试,应用try……exce框架的便利;最后,当然是提醒了我要注意编程习惯的良好养成。
2.然后,就是遇到的第二个问题,在调试助手上模拟的客户端断开连接之后,我的程序执行结果仍在输出
客户端没有数据到来,理论上,根据四次挥手,在程序中,断开连接的时候,应该是走到else,而不是except,因为recv_data应该是收到了,只不过是空字符串而已,那么,为什么会产生异常,认为是连接还在?反复检查代码之后,考虑可能是由于调试助手的原因,点击断开,是不是真正的断开了连接?事实证明我的猜想是正确的。是调试助手的小bug,需要把调试助手整个关闭,连接才真正断开,仅断开、关闭模拟客户端的界面,并没有真正关闭连接。最后,贴上让人感动的正确结果