简体   繁体   English

如何使用 sockets 同时从客户端向服务器发送 2 个变量 [Python]

[英]How to send 2 variables from a client to the server at the same time using sockets [Python]

Client.py客户端.py

import socket

HOST = '127.0.0.1'
PORT = 65432

ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789.'
message  = 'This message is encrypted using Caesar cipher'        

def encryptstr(message,key):
    #creating a list for new letters
    newletters = []
    #loop to assign new letter value
    for letter in message:
        uppercase = letter.isupper()
        letter = letter.lower()
    #checking if the letter is upper or lower
        if letter in ALPHABET:
            index = ALPHABET.find(letter)
            newindex = (index + key) % len(ALPHABET)
            letter = ALPHABET[newindex]
            if uppercase:
                letter = letter.upper()
        newletters.append(letter)
    #joining the list
    return ''.join(newletters)

def findkey():
    return 10

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  s.connect((HOST, PORT))
  test = encryptstr(message, findkey())
  s.sendall(test.encode()) # How to send key of 10 and Message?
  data = s.recv(1024)
  print('Received', data.decode())

Server.py服务器.py

import socket

HOST = '127.0.0.1'
PORT = 65432

ALPHABET = 'abcdefghijklmnopqrstuvwxyz0123456789.'

def decryptstr(message,key):
    #creating a list for new letters
    newletters = []
    #loop to assign new letter value
    for letter in message:
        uppercase = letter.isupper()
        letter = letter.lower()
    #checking if the letter is upper or lower
        if letter in ALPHABET:
            index = ALPHABET.find(letter)
            newindex = (index - key) % len(ALPHABET)
            letter = ALPHABET[newindex]
            if uppercase:
                letter = letter.upper()
        newletters.append(letter)
    #joining the list
    return ''.join(newletters)
  
def findkey():
  return 10

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
  s.bind((HOST, PORT))
  s.listen()
  conn, addr = s.accept()
  with conn:
    print('Connected by', addr)
    while True:
      data = conn.recv(1024)
      message = str(data.decode())
      test = decryptstr(message, findkey())
      print(test)
      if not data:
        break
      conn.sendall(data)

What I am trying to do with this current code is from the server.我试图用这个当前代码做的是来自服务器。 I wish to send the variable of "10" to the client.我希望将变量“10”发送给客户端。 From the client to the server, I would like to send back two variables, one being "10" and the other being the encrypted message.从客户端到服务器,我想发回两个变量,一个是“10”,另一个是加密消息。 After both are sent, I would like the server to decrypt the message and print it on the screen.两者都发送后,我希望服务器解密消息并将其打印在屏幕上。

You may be wondering why I would send the ten back and forth, and this is because I will be incorporating this Diffie-Hellman Example with the def findkeys() , Thank you for your time!您可能想知道为什么我会来回发送 10 个,这是因为我将把这个Diffie-Hellman 示例def findkeys()结合起来,谢谢您的宝贵时间!

client...客户...

sock.send(b'10'+encrypted_message)

server服务器

msg = sock.recv(1024)
code = msg[:2]
encrypted_payload = msg[2:]

Is this what you mean?你是这个意思吗?

You can send that var in json instead of single var it could help you to send more then 1 data at same time您可以在 json 中发送该 var 而不是单个 var 它可以帮助您同时发送超过 1 个数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 sockets 从客户端向服务器发送日期和时间 - How to send date and time from client to server using sockets 如何使用套接字从服务器向客户端发送消息 - How to send a message from the server to a client using sockets 使用 sockets 将消息从 Java 客户端发送到 Python 服务器,反之亦然 - Send message from Java client to Python server using sockets and vice versa 如何向客户端发送服务器响应? (Python套接字) - How do I send a server response to the client? (Python sockets) 如何同时发送和接收消息python sockets - How to send and recieve messages at the same time python sockets 使用 sockets 将字符串从一台计算机上的 C++ 客户端发送到另一台计算机上的 Python 服务器。 获取`发送:错误的文件描述符` - Using sockets to send a string from a C++ client on one computer to a Python server on another. Getting `send: Bad file descriptor` Python:如何随时从服务器向客户端发送消息? - Python: How to send message to client from server at any time? 套接字服务器和客户端在相同的python脚本中 - Sockets server and client in the same python script Python TCP sockets:客户端和服务器ZFFE33A3F6E35505FABA01D17FD07D641可以在同一线程上运行吗? - Python TCP sockets: can client and server sockets run on the same thread? 使用python套接字将Txt文件从客户端发送到服务器 - Sending Txt file to server from client using python sockets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM