I am trying to make an application with python that allows users to connect to a server and chat with each other. The users have to enter a Host(IP) and port, then press a button to connect to a server. The buttons and boxes are done with tkinter. When I use a tkinter button to connect to a server It returns:
TypeError: str, bytes or bytearray expected, not int
Trying to connect with something like this works:
HOST = input("Enter Host: ")
The relevant code is as follows:
#To connect to a server host
def connect():
HOST = etyHost.get()
PORT = etyPort.get()
if isinstance(HOST, str) == True:
print ("Yes")
else:
print ("NO")
print(HOST, PORT)
if not PORT:
PORT = 33000 # Default value.
else:
PORT = int(PORT)
client_socket.connect(ADDR)
#Entry box
etyHost = Entry(jahchat)
etyHost.pack()
etyHost.place(x = 0, y = 250)
#Entry box
etyPort = Entry(jahchat)
etyPort.pack()
etyPort.place(x = 0, y = 275)
#Button
btnConnect = Button(jahchat, text = "Connect", command = connect)
btnConnect.config(width = 20)
btnConnect.place(x = 0, y = 320)
HOST = 1
PORT = 1
client_socket = socket(AF_INET, SOCK_STREAM)
BUFSIZ = 1024
ADDR = (HOST, PORT)
I have tried using str() to convert the port and Ip integers to strings. Python recognizes it as a string but the issue is not resolved. All answers are appreciated.
You must first convert the string that contains the IP address into a byte or a string of bytes and then start communicating. According to the code below, your error will be resolved. Make sure your code is working correctly overall.
string = '192.168.1.102'
new_string = bytearray(string,"ascii")
ip_receiver = new_string
s.sendto(text.encode(), (ip_receiver, 5252))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.