【问题标题】:Exception has occurred: OSError [WinError 10057] A request to send or receive data was disallowed because the socket is not connected发生异常:OSError [WinError 10057] 不允许发送或接收数据的请求,因为未连接套接字
【发布时间】:2021-10-11 13:46:44
【问题描述】:

我正在尝试将文件从客户端传输到服务器。但由于错误,我没有收到任何文件。

这是我的 server.py 文件,但是当我即将从客户端接收文件时出现此错误。

import socket
import pyautogui
from pyautogui import *
from tkinter import *
from tkinter import filedialog
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0',4321))
s.listen(5)

root = Tk()
root.withdraw()


while True:
    clientsocket, address = s.accept()
    print(f"Connecting from {address} has been established")

    msg = clientsocket.recv(1024)
    print(msg.decode("utf-8"))

    file_name = 'recieved file.py'
    file = open(file_name, 'wb')
    file_data = s.recv(1024)
    file.write(file_data)
    file.close()
    pyautogui.alert("File recieved successfully!")

这是我下面的client.py。不过这不会出错。

import socket
import pyautogui
from tkinter import *
from tkinter import filedialog
import os
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.0.108',4321))

while True:
    confirm_send = pyautogui.confirm("Do you want to start file transfer?")
    if confirm_send == 'OK':

        file = filedialog.askopenfilename()
        file_name = os.path.basename(file)

        open_file = (file, 'rb')        

        with open(file) as in_file:
            file_data = in_file.read()
            s.send(file_data)

        pyautogui.alert("File Transferred succefully!!")

这是我对 server.py 的错误

Exception has occurred: OSError
[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
  File "D:\Workspace\Code\Cloud Storage\server.py", line 25, in <module>
    file_data = s.recv(1024)

【问题讨论】:

    标签: python sockets file-transfer pyautogui


    【解决方案1】:

    您的端口可能不可用。您是否尝试过 49152–65535 范围内的端口?

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 2013-02-03
      • 2014-12-20
      • 2014-08-30
      • 2013-11-24
      • 2021-08-16
      • 1970-01-01
      • 2018-07-01
      • 2018-06-20
      相关资源
      最近更新 更多