使用 python Websockets 从 android studio 接收图像时“未收到有效的 HTTP 响应”

Ade*_*afa 6 python java websocket

我正在尝试构建一个 WebSockets 客户端,该客户端接收从使用(android studio)使用本文档提供的简单示例构建的移动应用程序发送的图像。但是当我将 IP 地址和端口号替换为移动应用程序提供的 IP 地址和端口号时,出现以下错误:

C:\Users\LENOVO\Anaconda3\python.exe "D:/SBME/4th year/1at term/Electronics/Task3/Wclient.py"
Traceback (most recent call last):
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\http.py", line 139, in read_response
    status_line = await read_line(stream)
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\http.py", line 219, in read_line
    raise EOFError("line without CRLF")
EOFError: line without CRLF

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 101, in read_http_response
    status_code, reason, headers = await read_response(self.reader)
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\http.py", line 141, in read_response
    raise EOFError("connection closed while reading HTTP status line") from exc
EOFError: connection closed while reading HTTP status line

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:/SBME/4th year/1at term/Electronics/Task3/Wclient.py", line 250, in <module>
    asyncio.get_event_loop().run_until_complete(rcv_img())
  File "C:\Users\LENOVO\Anaconda3\lib\asyncio\base_events.py", line 573, in run_until_complete
    return future.result()
  File "D:/SBME/4th year/1at term/Electronics/Task3/Wclient.py", line 10, in rcv_img
    async with websockets.connect(uri) as websocket:
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 517, in __aenter__
    return await self
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 547, in __await_impl__
    extra_headers=protocol.extra_headers,
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 290, in handshake
    status_code, response_headers = await self.read_http_response()
  File "C:\Users\LENOVO\Anaconda3\lib\site-packages\websockets\client.py", line 103, in read_http_response
    raise InvalidMessage("did not receive a valid HTTP response") from exc
websockets.exceptions.InvalidMessage: did not receive a valid HTTP response
Run Code Online (Sandbox Code Playgroud)

我搜索了导致此错误的原因,但不幸的是,我无法在互联网或文档上找到答案。

这是简单的Python代码

import asyncio
import websockets

async def rcv_img():
    uri = "ws://192.168.1.8:8080"
    async with websockets.connect(uri) as websocket:
       
        img_rcv = await websocket.recv()
        # some image processing code here

asyncio.get_event_loop().run_until_complete(rcv_img())
Run Code Online (Sandbox Code Playgroud)

以及移动应用程序中的发送部分

public class FileTxThread extends Thread {
        Socket socket;

        FileTxThread(Socket socket){
            this.socket= socket;
        }

        //@RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void run() {
           File file  = new File(Environment.getExternalStorageDirectory(), "Frame.jpg");
           WriteByteArray.toFile(imagebyte,file);

            byte[] bytes = new byte[(int) file.length()];
            BufferedInputStream bis;
            try {
                bis = new BufferedInputStream(new FileInputStream(file));
                bis.read(bytes, 0,bytes.length);

                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(bytes);
                oos.flush();
                socket.close();
                final String sentMsg = "File sent to: " + socket.getInetAddress();
                
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,
                                sentMsg,
                                Toast.LENGTH_LONG).show();
                    }});

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }

Run Code Online (Sandbox Code Playgroud)