2 years ago
#58008
Mouad Idi
Python3:socket:TypeError: unsupported operand type(s) for +=: 'type' and 'bytes'
I am trying to use the python socket package to implement an echo server. But it continuously occurs the error: TypeError: unsupported operand type(s) for += 'type' and 'bytes', is there any errors in my code? Can someone help me to fix it plsssss
here is the error:
C:\Users\mouad\AppData\Local\Programs\Python\Python310\python.exe "C:\Users\mouad\OneDrive\Bureau\Python Projects\Server Socket\server.py"
Waiting for connection .....
Client IP 192.168.1.12 : 56952
>> getmac
Traceback (most recent call last):
File "C:\Users\mouad\OneDrive\Bureau\Python Projects\Server Socket\server.py", line 31, in <module>
full_result += chunk
TypeError: unsupported operand type(s) for +=: 'type' and 'bytes'
Process finished with exit code 1
Here is my code:
server.py:
import socket
end_result = "<end_of_result>"
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_connection = ("192.168.1.12", 2000)
print('Waiting for connection .....')
server_socket.bind(server_connection)
server_socket.listen(50)
server_socket, address_client = server_socket.accept()
print('Client IP', address_client[0], ":", address_client[1])
while True:
command = input(">> ")
server_socket.send(command.encode())
if command.lower() == "stop":
server_socket.close()
break
elif command == "":
continue
else:
full_result = bytes
while True:
chunk = server_socket.recv(1024)
if chunk.endswith(end_result.encode()):
chunk = chunk[:-len(end_result)]
full_result += chunk
print(full_result.decode())
break
else:
full_result += chunk
#print(full_result.decode())
client.py:
import socket
import subprocess
import time
server_connection = ("192.168.1.12", 2000)
end_result = "<end_of_result>"
while True:
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('Connecting ...')
client_socket.connect(server_connection)
while True:
command = client_socket.recv(1024)
command = command.decode()
if command.lower() == 'stop':
client_socket.close()
break
elif command == '':
continue
else:
output = subprocess.run(["powershell.exe", command], shell=True, capture_output=True)
if output.stderr.decode("utf-8") == "":
result = output.stdout
result = result.decode("utf-8")+end_result
result = result.encode("utf-8")
elif output.stderr.decode("utf-8") != "":
result = output.stderr
result = result.decode("utf-8")+end_result
result = result.encode("utf-8")
client_socket.sendall(result.encode("utf-8"))
except Exception:
print('.')
time.sleep(0.5)
print('..')
time.sleep(0.5)
print('...')
time.sleep(3)
print("Can't connect ")
time.sleep(0.5)
python-3.x
sockets
serversocket
0 Answers
Your Answer