【发布时间】:2017-01-18 23:25:49
【问题描述】:
我正在尝试使用以下代码编写服务器。它是线程化的,我需要帮助的只是这个错误。
Unhandled exception in thread started by <function threaded_client at 0x0000000003302158>
line 28, in threaded_client
data = conn.recv(2048)
OSError: [WinError 10038] An operation was attempted on something that is not a socket
这个错误我无法解决并且已经尝试解决。我真的很想知道如何解决它。
import socket
import sys
from _thread import *
import time
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 for a Connection...")
def threaded_client(conn):
conn.send(str.encode("Welcome, please write to the server.\r\n"))
userinput = ""
while True:
data = conn.recv(2048)
data = data.decode('utf-8')
if not data:
break
#data = data.decode('utf-8')
#Gather up the Input
if data == '\r\n':
userinput += data
#Do something with it
else:
reply = 'Server output: ' + userinput
conn.sendall(str.encode(reply))
userinput = ""
conn.close()
while True:
conn, addr = s.accept()
print("connected to: " +addr[0] + ': ' + str(addr[1]))
start_new_thread(threaded_client, (conn,))
现在我遇到了服务器与客户端交互方式的问题。我的 CMD 窗口的图像在下面打开。请提供修复代码。 对于 Windows 8,请。
【问题讨论】:
-
我会在早上 - 现在已经很晚了,所以不要指望马上回复!
标签: python python-3.x runtime-error