Сервер:
Код: Выделить всё
from Rest_Endpoint.thread import ConnectionThread
HOST = ''
PORT = 6000
def start_server():
server: socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
server.bind((HOST,PORT))
server.listen(10)
print('listen to port', PORT)
server.setblocking(False)
while True:
try:
conn, addr = server.accept() # Will throw BlockingIOError if no connections
print('Connected by:', addr)
thread_spawn(conn, addr)
except BlockingIOError:
# No incoming connection; continue the loop
continue
except Exception as e:
print(f'Unexpected error: {e}')
break
Код: Выделить всё
from Rest_Endpoint.thread import ConnectionThread
HOST = ''
PORT = 6000
def start_server():
server: socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
server.bind((HOST,PORT))
server.listen(10)
print('listen to port', PORT)
server.setblocking(False)
while True:
try:
conn, addr = server.accept() # Will throw BlockingIOError if no connections
print('Connected by:', addr)
thread_spawn(conn, addr)
except BlockingIOError:
# No incoming connection; continue the loop
continue
except Exception as e:
print(f'Unexpected error: {e}')
break
Код: Выделить всё
# Echo client program
import socket
HOST = '' # The remote host
PORT = 6000 # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
# Connect to the server
s.connect((HOST, PORT))
print(f'Connected to server at {HOST}:{PORT}')
while True:
# message = input()
# bmessage = (message + '\n').encode('utf-8')
s.sendall(b'test')
# print(bmessage)'
except ConnectionRefusedError:
print(f"Could not connect to server at {HOST}:{PORT}. Ensure the server is running.")
except Exception as e:
print(f"An error occurred: {e}")
Код: Выделить всё
Connected by ('127.0.0.1', 51804)
waiting for message
prints data:
b'testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest'
но когда я пытаюсь переключить клиентский код на:
Код: Выделить всё
while True:
message = input()
bmessage = (message).encode('utf-8')
s.sendall(b'test')
print(bmessage)
Может быть, кто-нибудь поможет мне с объяснением.
Заранее спасибо.
Подробнее здесь: https://stackoverflow.com/questions/793 ... ata-python
Мобильная версия