python-3.xlistsocketsmemoryview

Python throw error byte like object required not list


I am trying to connect the socket via the following code

    try:
        # create an INET, STREAMing socket
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # now connect to the web server on port 80 - the normal http port
        self.client.connect((host, port))
        user = ['User.loginByToken', access_token]
        self.client.sendall(user)
        # self.client._locust_environment = self.environment
    except Exception as e:
        color_print("\n[!] Check Server Address or Port", color="red", underline=True)

it throws an error memoryview: a bytes-like object is required, not 'list'. what can I do to solve it?


Solution

  • You need to convert user var to bytes in order to send thru a socket: You can try this:

    import json
    
    user = json.dumps({'User.loginByToken': access_token})
    user = user.encode("utf-8")