当前位置: 首页 > article >正文

【Python】打造自己的HTTP server

词汇汇总

CRLF 指的是换行和回车\r\n

教程

./your_program.sh #启动自己的服务

curl -v http://localhost:4221#开启另一个终端 测试
  • HTTP response
    An HTTP response is made up of three parts, each separated by a CRLF (\r\n):
    Status line.
    Zero or more headers, each ending with a CRLF.
    Optional response body.
    先来一个简单的,只包含状态行。
// Status line
HTTP/1.1  // HTTP version
200       // Status code
OK        // Optional reason phrase
\r\n      // CRLF that marks the end of the status line

// Headers (empty)
\r\n      // CRLF that marks the end of the headers

// Response body (empty)

#main.py
import socket  # noqa: F401

def main():
    # You can use print statements as follows for debugging, they'll be visible when running tests.
    print("Logs from your program will appear here!")

    # Uncomment this to pass the first stage
    #
    server_socket = socket.create_server(("localhost", 4221), reuse_port=True)
    server_socket.accept() # wait for client
    server_socket.accept()[0].sendall(b"HTTP/1.1 200 OK\r\n\r\n")

if __name__ == "__main__":
    main()
#your_program.sh
#通过执行./your_program.sh执行这个文件
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec pipenv run python3 -m app.main "$@"


http://www.kler.cn/a/556697.html

相关文章:

  • OpenGauss MySQL兼容库迁移
  • Java 中 HTTP 协议版本使用情况剖析
  • 【QT 网络编程】HTTP协议(二)
  • SQLMesh 系列教程6- 详解 Python 模型
  • 物联网与大数据:揭秘万物互联的新纪元
  • (网络安全)渗透测试
  • 想象一个AI保姆机器人使用场景分析
  • 如何使用 JavaScript 模拟 Docker 中的 UnionFS 技术:从容器到文件系统的映射
  • 8.python文件
  • 纳米科技新突破:AbMole助力探索主动脉夹层的基因密码
  • Java 同步锁性能的最佳实践:从理论到实践的完整指南
  • Java中JDK、JRE,JVM之间的关系
  • 【全栈】SprintBoot+vue3迷你商城(12)
  • 企业商业秘密百问百答之五十三【商业秘密转让】
  • 【目标检测】【PANet】Path Aggregation Network for Instance Segmentation
  • 九联UNT403AS_晶晨S905L3S芯片_2+8G_安卓9.0_卡刷固件包
  • R语言安装生物信息数据库包
  • 一篇搞懂vue3中如何使用ref、reactive实现响应式数据
  • Ubuntu22.04.6如何固定ip地址
  • Webpack打包优化