这是我在Python中创建的套接字服务器,但是当我运行它时,它突出显示了threaded_client中的while循环并且出现了一个警告框。它说'语法无效'。任何人都可以告诉我发生了什么。此外,当我注释掉任何内容时,错误只会移动到下一行。这是代码:
import socket
import sys
from _thread import *
host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
except socket.error as e:
print(str(e))
s.listen(5)
print('Waiting...')
def threaded_client(conn):
conn.send(str.encode('TYPE HERE\n')
while True:
data = conn.recv(2048)
reply = 'Server output: '+ data.decode('utf-8')
if not data:
break
conn.sendall(str.encode(reply))
conn.close()
while True:
conn, addr = s.accept()
print('Connected to: '+addr[0]+':'+str(addr[1]))
start_new_thread(threaded_client,(conn,)
答案 0 :(得分:0)
您缺少括号
from thread import *
import socket
import sys
host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
except socket.error as e:
print(str(e))
s.listen(5)
print('Waiting...')
def threadedClient(conn):
conn.send(str.encode("Type Here\n"))
while True:
data = conn.recv(2048)
reply = 'Server output: '+ data.decode('utf-8')
if not data:
break
conn.sendall(str.encode(reply))
conn.close()
while True:
conn, addr = s.accept()
print('Connected to: '+addr[0]+':'+str(addr[1]))
start_new_thread(threaded_client,(conn,))