我想使服务器同时返回文本和图像,仅使用python套接字,而不使用诸如simplehttpserver之类的模块

时间:2018-11-10 10:39:14

标签: html serversocket python-sockets

问题是我无法获取图像文件

  1. 我应该将图像文件编码为binaryarras吗?
  2. 如何同时使用utf-8和base64来使图像和文本同时显示...

    ##
    import socket
    import base64
    
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    server_socket.bind(('127.0.0.1', 8080))
    
    server_socket.listen(1)
    print('listening')
    
    while True:
        connect_socket, addr = server_socket.accept()
        data = connect_socket.recv(99999)
    
        print('data received : ',data.decode())
    
        connect_socket.sendall("""HTTP/1.1 200 OK
    
        <head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
              <meta http-equiv="Content-Type" content="image/jpeg;charset=UTF-8" />
            <title>Happy pig</title>
        </head>
        <body>
           <font color=#55C0F1 size=10>
           <h1>
                "Hi"
                <img src="pig.jpg" width= "137" height="150">
           </h1>
           </font>
        </form>
        </body>
        </html>""".encode('utf-8'))
    
    connect_socket.close()
    
    ##
    

0 个答案:

没有答案