http与tcp
Posted 人一直在变
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了http与tcp相关的知识,希望对你有一定的参考价值。
浏览器请求
GET / HTTP/1.1
服务器响应
1. header
HTTP/1.1 200 OK
2. body
<h1>hahhh</h1>
# 用python写服务端等待浏览器的请求,给一个简单的响应
import socket
def service_client(new_socket):
request = new_socker.recv(1024)
print(request)
response = "HTTP/1.1 200 OK\r\n"
response += "\r\n"
response += "hahaahh"
new_socket.send(response.encode("utf-8"))
new_socket.close()
def main():
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcp_server_socket.bind("", 7890)
tcp_server_socket.listen(128)
while True:
new_socket, client_addr = tcp_server_socket.accept()
service_client(new_socket)
tcp_server_socket.close()
if __name__ == "__main__":
main()
服务器先调close先关,要等待2min
不用换端口,加
SO_REUSEADDR,就不用再等待2min
客户端不绑定端口,先关,换端口不用等待
以上是关于http与tcp的主要内容,如果未能解决你的问题,请参考以下文章