![](/img/trans.png)
[英]python socket programming OSError: [WinError 10038] an operation was attempted on something that is not a socket
[英]I am getting this error in my python code. "OSError: [WinError 10038] An operation was attempted on something that is not a socket"
在我的 python 項目中,我有一個工作服務器。 現在我正在制作客戶端,但我不斷收到“OSError:[WinError 10038] 嘗試對不是套接字的東西進行操作”錯誤。 我尋找解決方案,但其中大多數涉及人員代碼在 while 循環內,他們忘記關閉連接或類似的東西。 我的代碼不是那樣的,我找不到解決方案。 因此,如果有人知道答案和答案的解釋,請提供幫助。 順便說一句,我正在關注這個教程。
我在第 18 行的 send_message() function 上收到錯誤消息。
這是我的代碼(我有一個 working.kv 文件,但如果需要,我也可以發布它):
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
import socket
import threading
kivy.require("1.9.0")
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
class MyRoot(BoxLayout):
def __init__(self):
super(MyRoot, self).__init__()
def send_message(self):
client.send(f"{self.nickname_text.text}: {self.message_text.text}".encode("utf-8"))
def connect_to_server(self):
if self.nickname_text != "":
client.connect((self.ip_text.text, 9999))
message = client.recv(1024).decode("utf-8")
if message == "NICK":
client.send(self.nickname_text.text.encode("utf-8"))
self.send_btn.disabled = False
self.message_text.disabled = False
self.connect_btn.disabled = True
self.ip_text.disabled = True
self.make_invisible(self.connection_grid)
self.make_invisible(self.connect_btn)
thread = threading.Thread(target=self.receive)
thread.start()
def make_invisible(self, widget):
widget.visible = False
widget.size_hint_x = None
widget.size_hint_y = None
widget.height = 0
widget.width = 0
widget.text = ""
widget.opacity = 0
def receive(self):
stop = False
while not stop:
try:
message = client.recv(1024).decode("utf-8")
self.chat_text.text += message + "\n"
except:
print("ERROR")
client.close()
stop = True
class WebChat(App):
def build(self):
return MyRoot()
webChat = WebChat()
webChat.run()
我又做了一些調試,結果發現原因是我安裝了錯誤的 kivy 版本。 感謝所有試圖提供幫助的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.