-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
115 lines (104 loc) · 4.03 KB
/
server.py
File metadata and controls
115 lines (104 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import subprocess
import socket
import threading
import os
import ssl
os.environ['PYDEVD_DISABLE_FILE_VALIDATION'] = '1'
def send_certificate(client_socket):
# Path to the server's certificate file
cert_file_path = "server.crt"
# Open the certificate file and read its contents
with open(cert_file_path, "rb") as cert_file:
certificate_data = cert_file.read()
# Send the certificate data to the client
client_socket.sendall(certificate_data)
def main():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 9999
ipaddress = "localhost" #use ip address here
server.bind((ipaddress, port))
server.listen()
os.system('cls')
print(f"Server is listening at port: {port}")
print("Clients: ")
while True:
client, addr = server.accept()
host = addr
send_certificate(client)
try:
# Wrap the client socket with SSL
server_ssl = ssl.wrap_socket(
client,
server_side=True,
certfile="./server.crt",
keyfile="./server.key",
ssl_version=ssl.PROTOCOL_TLSv1_2
)
file_name = receive_file_name(server_ssl)
thread = threading.Thread(target=receivefile, args=(file_name, server_ssl,host))
thread.start()
print(f"Active clients: {threading.activeCount()-1}")
except ssl.SSLError as e:
print(f"SSL Error: {e}")
def receive_file_name(client_socket):
file_name = b"" # Initialize an empty byte string to store received data
while True:
data = client_socket.recv(12)
if not data:
break
file_name += data # Concatenate the received data
if b"<END_FILENAME>" in file_name: # Check if the end marker is present in the received data
file_name = file_name.replace(b"<END_FILENAME>", b"") # Remove the end marker
break
response_message = f"Filename {file_name.decode()} received successfully!"
client_socket.sendall(response_message.encode())
return file_name.decode() # Decode the byte string to get the file name as a string
def compile_and_run(filename):
error = None
output = None
try:
# Execute the code and capture the output
if filename.endswith(".py"):
result = subprocess.run(['python', filename], capture_output=True, text=True, timeout=10)
output = result.stdout
error = result.stderr
elif filename.endswith(".c"):
compilation = subprocess.run(['gcc', '-o', filename[:-2], filename], capture_output=True, text=True, timeout=10)
if (compilation.returncode == 0):
executable = subprocess.run([filename[:-2] + '.exe'], capture_output=True, text=True, timeout=10)
output = executable.stdout
error = executable.stderr
elif filename.endswith(".rs"):
result = subprocess.run(['rustc', filename], capture_output=True, text=True, timeout=10)
output= result.stdout
error = result.stderr
else:
pass
if error:
return error
else:
return output
except subprocess.TimeoutExpired:
return "Error: Timeout expired. Execution took too long."
except Exception as e:
return f"Error: {str(e)}"
def receivefile(file_name,client,host):
print("Connection from {}:{}\n".format(host[0],host[1]))
print("Receiving:\n", file_name)
file = open(file_name, "wb")
while True:
data = client.recv(1024)
if not data:
break
if data.endswith(b"<END>"):
file.write(data[:-5]) # Remove the "<END>" marker before writing to file
break
else:
file.write(data)
file.close()
response_message = "File received successfully!"
client.sendall(response_message.encode())
output_final = compile_and_run(file_name)
compile_and_run(file_name)
client.sendall(output_final.encode())
main()