因为图片读取以后是二进制编码,所以有两种方法可以发送图片:
- 将http响应头转为字节码,然后和图片二进制文件打包发送
- 先发送http响应头,然后发送图片二进制文件
第一种方法:
# 编写自己的web服务器,发送图片
# http是给予TCP实现的
import socket
def service_client(new_socket):
request = new_socket.recv(1024).decode("utf-8")
print(request)
response_head = "HTTP/1.1 200 OK\r\n"
# 分割http响应头和响应body
response_head += '\r\n'
response_body = read_img('1.jpg')
data = response_head.encode("utf-8") + response_body
new_socket.send(data)
new_socket.close()
def main():
# 1. 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定
tcp_server_socket.bind(("", 9090))
# 3. 监听套接字
tcp_server_socket.listen(128)
while True:
# 4. 等待连接
new_socket, client_addr = tcp_server_socket.accept()
# 5. 发送数据
service_client(new_socket)
# 关闭监听套接字
tcp_server_socket.close()
def read_img(file_path):
file = open(file_path, 'rb')
file_data = file.read()
file.close()
return file_data
if __name__ == '__main__':
main()
第二种方法:
# 编写自己的web服务器,发送图片
# http是给予TCP实现的
import socket
def service_client(new_socket):
request = new_socket.recv(1024).decode("utf-8")
print(request)
response_head = "HTTP/1.1 200 OK\r\n"
# 分割http响应头和响应body
response_head += '\r\n'
response_body = read_img('1.jpg')
new_socket.send(response_head.encode("utf-8"))
new_socket.send(response_body)
new_socket.close()
def main():
# 1. 创建套接字
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. 绑定
tcp_server_socket.bind(("", 9091))
# 3. 监听套接字
tcp_server_socket.listen(128)
while True:
# 4. 等待连接
new_socket, client_addr = tcp_server_socket.accept()
# 5. 发送数据
service_client(new_socket)
# 关闭监听套接字
tcp_server_socket.close()
def read_img(file_path):
file = open(file_path, 'rb')
file_data = file.read()
file.close()
return file_data
if __name__ == '__main__':
main()
效果展示: