词汇汇总
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.
先来一个简单的,只包含状态行。
python">// 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 "$@"