python flasksocketio TypeError:需要类似字节的对象,而不是“str”

2024-04-26 15:02:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始用Socketio,我遇到了一个问题。我不能给我的烧瓶会议发一个简单的信息。我的Java代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;

public class MainActivity extends AppCompatActivity {

    Socket socket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            socket = IO.socket("http://192.168.8.101:8080/send");
        } catch (URISyntaxException e) {

        }

    }

    public void TestButton(View v){
        Log.d("send","before send");
        JSONObject obj = new JSONObject();
        try {
            obj.put("message", "hi");
            obj.put("binary", new byte[42]);
            socket.emit("mes", obj);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

我的Python代码:

^{pr2}$

错误:

TypeError: a bytes-like object is required, not 'str' <Greenlet at 0x24118773178: _handle_and_close_when_done(<bound method WSGIServer.handle of <WSGIServer at , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed]  object, fd=-1, )> failed with TypeError

我希望有人能帮我解决这个问题。我到处找遍了,却找不到我的答案。在


Tags: 代码orgimportsendlogviewjsonobj
1条回答
网友
1楼 · 发布于 2024-04-26 15:02:59

我认为你可以在gevent和gevent websocket插件中使用python3。 gevent websocket还不支持python3。在

你必须在这里选择:

  1. 卸载gevent和gevent websocket并使用eventlet。在
  2. 修复代码geventwebsocket/处理程序.py在236号线。在

    if b'101' not in self.status:
    

我希望我能帮助你。在

相关问题 更多 >