如何使用套接字同时从客户端向服务器发送 2 个变量 [Python]
How to send 2 variables from a client to the server at the same time using sockets [Python]
Client.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
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)
我尝试使用当前代码执行的操作来自服务器。我希望将变量“10”发送给客户端。从客户端到服务器,我想发回两个变量,一个是“10”,另一个是加密消息。两者发送后,我希望服务器解密消息并将其打印在屏幕上。
您可能想知道为什么我会来回发送 10 个,这是因为我会将 Diffie-Hellman Example 与 def findkeys()
合并,感谢您的宝贵时间!
客户...
sock.send(b'10'+encrypted_message)
服务器
msg = sock.recv(1024)
code = msg[:2]
encrypted_payload = msg[2:]
你是这个意思吗?
您可以在 json 中发送该 var 而不是单个 var 它可以帮助您同时发送多于 1 个数据
Client.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
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)
我尝试使用当前代码执行的操作来自服务器。我希望将变量“10”发送给客户端。从客户端到服务器,我想发回两个变量,一个是“10”,另一个是加密消息。两者发送后,我希望服务器解密消息并将其打印在屏幕上。
您可能想知道为什么我会来回发送 10 个,这是因为我会将 Diffie-Hellman Example 与 def findkeys()
合并,感谢您的宝贵时间!
客户...
sock.send(b'10'+encrypted_message)
服务器
msg = sock.recv(1024)
code = msg[:2]
encrypted_payload = msg[2:]
你是这个意思吗?
您可以在 json 中发送该 var 而不是单个 var 它可以帮助您同时发送多于 1 个数据