python ssl server send variable to function
Ok my Python SSL server/client works as far as receiving data (as coded below from a online example). I have two questions if any could enlighten me.
I want to use the "data" var on the server side in another function use this var in a query or some sort of sub-string effect. What is the best way to send this to another function and then return it to the echo_client() ?
best methods to send data back threw to the client as a response string ect?
import socket from socket import AF_INET, SOCK_STREAM, SO_REUSEADDR, SOL_SOCKET, SHUT_RDWR import ssl global_card_data = [] KEYFILE = 'localhost.key' CERTFILE = 'localhost.cert'
def echo_client(s): while True: data = s.recv(8192) print(data.decode("utf-8"))
if data == b'':
break
s.send(b'This is a response.')
print('Connection closed')
s.close()
def echo_server(address): s = socket.socket(AF_INET, SOCK_STREAM) s.bind(address) s.listen(1) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s_ssl = ssl.wrap_socket(s, keyfile=KEYFILE, certfile=CERTFILE, server_side=True)
while True:
try:
(c,a) = s_ssl.accept()
print('Got connection', c, a)
echo_client(c)
except socket.error as e:
print('Error: {0}'.format(e))
echo_server((socket.gethostbyname('localhost'), 8082))
上一篇: ssl.SSLZeroReturnError:TLS / SSL连接已关闭(EOF)(
下一篇: python ssl服务器发送变量到函数