Skip to content

Commit

Permalink
改进了 validate_user 函数的实现
Browse files Browse the repository at this point in the history
  • Loading branch information
HBVdBY4N9 committed Jan 9, 2025
1 parent 4f7f3b4 commit f5edd5d
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions AWS_EC2/server_ec2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import socket
from threading import Thread, activeCount
import ec2_process
from pickle import dumps as pickle_dumps, loads as pickle_loads
from time import sleep, perf_counter

# AWS SERVER CONFIG
FORMAT = 'utf-8'
HEADER = 64
PORT = 5555
SERVER = socket.gethostbyname(socket.gethostname()) # Local automatically will connect to AWS-ELB
ADDR = (SERVER, PORT)

attempts = 1
while attempts < 20:
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
print("[SET UP DONE] attempts: ", attempts)
break
except OSError:
print("[FAILURE] failed setup, attempt: ", attempts)
attempts += 1
sleep(3)


def handle_client(conn, addr):

connected = True
while connected:

# receiving
msg_length = conn.recv(HEADER).decode(FORMAT)

if not msg_length: # ELB Health checks - Disconnect after ping
print(f"[PING] {addr}")
connected = False

if msg_length: # first msg sent from client telling the server the length of upcoming msg
print(f"[MESSAGE RECEIVED] {addr}")

msg_length = int(msg_length)
msg = b'' # user inputs from GAE
while len(msg) < msg_length:
msg += conn.recv(msg_length) # receive the whole main msg as we know the size of it

user_inputs=pickle_loads(msg)

# process received msg
try:
start_time = perf_counter()
generated_res = ec2_process.generate_results(STOCK=user_inputs["STOCK"], A=user_inputs["A"], V=user_inputs["V"], S=user_inputs["S"], R=user_inputs["R"], Res_Type=user_inputs["Res_Type"]) # returns dict of pkls
finish_time = perf_counter()
print(f'[DONE CALCULATION] {addr} : Res_Type: {user_inputs["Res_Type"]}, R: {user_inputs["R"]}, Duration: {finish_time - start_time}')
status = "OK"

except:
print(f"[FAILED CALCULATION] {addr}")
status = "FAILED"


if status=="OK":
# sending results back
s_msg_length = len(generated_res)
s_send_length = str(s_msg_length).encode(FORMAT)
s_send_length += b' ' * (HEADER - len(s_send_length))
conn.send(s_send_length)
conn.send(generated_res)
connected = False

else:
# sending failure msg
fail_msg = pickle_dumps(status)
s_msg_length = len(fail_msg)
s_send_length = str(s_msg_length).encode(FORMAT)
s_send_length += b' ' * (HEADER - len(s_send_length))
conn.send(s_send_length)
conn.send(fail_msg)
connected = False

conn.close()


def start():
server.listen()
print(f"[LISTENING] Server is listening on {SERVER}\n")
while True:
conn, addr = server.accept()
t = Thread(target=handle_client, args=(conn, addr))
t.start()
print(f"[ACTIVE CONNECTIONS] {activeCount() - 1}")


print("[STARTING] server is starting...")
start()

0 comments on commit f5edd5d

Please sign in to comment.