I'm trying to write a chat program...well it doesn't work.
That's the code I wrote...I deleted everything unnecessary:
import socket
import time
import threading
import json
def reciving(name, sock, shutdown):
...
host = "127.0.0.1"
port = 0
server = ("127.0.0.1", 2300)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)
...
print("Welcome to my Chat")
alias = input("Name: ")
msg = input("-> ")
msg = json.dumps(msg)
alias = json.dumps(alias)
send_data = [msg, alias]
while msg != "q":
if msg != "":
send_data = json.dumps(send_data)
s.sendto(send_data, server)
msg = input("-> ")
msg = json.dumps(msg)
send_data = [msg, alias]
time.sleep(0.2)
shutdown = True
rT.join()
s.close()
If I start the program, first everything is fine but after writing sth i'll get that error message:
Console:
Welcome to my Chat
Name: S
-> hello //this is my input
Traceback (most recent call last):
File "client.py", line 40, in <module>
s.sendto(send_data, server)
TypeError: a bytes-like object is required, not 'str'
I think I've encoded the strings in the list and then the list itself, that's what I have to do, to send a list ... right? I've already tried to just encode the list and not the strings inside or to encode just the strings not the list ... or to encode the strings with utf-8 and the list with json...it won't work:(
So what and how do I have to encode that I can send it to the server/socket?