社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python:winerror 10045:引用的对象类型不支持尝试的操作

B.Uriah • 5 年前 • 2783 次点击  

我正在使用客户机/服务器程序创建一个caesar密码程序。客户端将输入消息和密钥,服务器将返回密码文本。这是我的服务器代码:

import socket

def getCaesar(message, key):
    cipher = "" 

    for i in message: 
        char = message[i] 

        # Encrypt uppercase characters 
        if (char.isupper()): 
            cipher += chr((ord(char) + key-65) % 26 + 65) 

        # Encrypt lowercase characters 
        else: 
            cipher += chr((ord(char) + key - 97) % 26 + 97) 

    return cipher 

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
port=4000

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

s.bind((host,port))
s.listen(5)
print("Listenting for requests")

while True:
    s,addr=s.accept()
    print("Got connection from ",addr)
    print("Receiving...")

    message,key=s.recv(1024)
    resp=getCaesar(message, key)

    s.send(resp)
s.close()

错误消息调用此行:s.send(消息,键),并显示以下错误:

oserror:[winerror 10045]引用的对象类型不支持尝试的操作。这个错误是什么意思?

我的客户代码:

import socket

def getMessage():
    print('Enter your message:')
    return input()

def getKey():
    key = 0
    while True:
        print('Enter the key number (1-%s)' % (26))
        key = int(input())
        if (key >= 1 and key <= 26):
            return key

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host=socket.gethostname()
port=4000
s.connect((host,port))


message = getMessage()
key = getKey()

message=message.encode()


s.send(message, key)
cipher= s.recv(1024)

print('Ciphertext: ')
print(cipher)
s.close()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43941
 
2783 次点击  
文章 [ 1 ]  |  最新文章 5 年前
sanyash
Reply   •   1 楼
sanyash    6 年前

请参阅帮助(socket.send):

Help on built-in function send:

send(...) method of socket.socket instance
    send(data[, flags]) -> count

    Send a data string to the socket.  For the optional flags
    argument, see the Unix manual.  Return the number of bytes
    sent; this may be less than len(data) if the network is busy.

所以,这条线 s.send(message, key) 可能无法按您预期的方式工作:它只发送 message 具有 key 解释为标志,而不是两者 消息 钥匙 . 尝试发送 消息 钥匙 分别地。别忘了 recv 它们也是分开的。