簡體   English   中英

OSError: [Errno 9] 錯誤的文件描述符

[英]OSError: [Errno 9] Bad file descriptor

我最初嘗試使用 python 運行但有不同的錯誤,所以我嘗試使用 python3 並收到標題中的錯誤。 我正在嘗試連接到服務器並下載已實現 tls 的文件。

import socket, ssl, pprint
import os, time
import threading

def main():
    s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    ssl_sock = ssl.wrap_socket(s2,
                           server_side = False,
                           ca_certs="CA.crt",
                           cert_reqs=ssl.CERT_REQUIRED)
    s2.connect(('localhost',10024))
    filename = raw_input("What file do you wish to download? -> ")

    if filename != 'q':
        s2.send(filename)

        data = s2.recv(1024)
        if data [:7] == 'fEXISTS':
            fSize = long(data[7:])

            message = raw_input("The file you wish to download is " +str(fSize)+\
                        "bytes, would you like to proceed? (y/n): ")

            if message == 'y':
                s2.send ('OK')
                f = open('new_'+filename, 'wb')
                data = s2.recv(2000000)
                totalRecv = len(data)
                f.write(data)

                while totalRecv < fSize:
                    data = s2.recv(2000000)
                    totalRecv += len(data)
                    f.write(data)
                    progress = ((totalRecv/float(fSize))*100)
                    print ("{0: .2F}".format(progress)+\
                            "% Completed")
        else:
            print ("ERROR: File does not exist!")
    s2.close()

if __name__ == '__main__':
    main()

將套接字包裝在 SSL 上下文中(使用ssl.wrap_socket )后,您不應再使用原始套接字。

您應該在ssl_sock上調用connectsendrecv等,而不是在s2

(具體而言,當你調用ssl.wrap_socket ,該.detach方法被稱為原始套接字從它刪除該文件描述上,該文件描述符傳送到SSL套接字實例。你可以用原來做的唯一事情就是那么關閉/摧毀它。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM