-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWarm-up Server.py
More file actions
71 lines (54 loc) · 3.31 KB
/
Copy pathWarm-up Server.py
File metadata and controls
71 lines (54 loc) · 3.31 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
from socket import * #Importerer Socket Modulen
global connection_socket
def start_server(): #Function to start the server and let the client connects
global connection_socket #Global type makes it available for use in functions as a local variable
server_socket = socket(AF_INET, SOCK_STREAM) # Connects to sockets: internett and TCP
server_socket.bind(("", 6666)) #Binds the client to the server
server_socket.listen(1) #1 spot in queue
print("Server ready for client connections")
connection_socket, client_address = server_socket.accept() #Accepts the 3-way handshake
print("Client connected from: ",client_address) #Prints clients stats
def read_request():
try:
global connection_socket #Global type makes it available for use in functions as a local variable
request_client = connection_socket.recv(1000).decode() #Receives the data from the client
request = request_client.strip("\n") #Strips newline
print("Request from Client: ",request)
return request #Returns the list with data from the client
except ConnectionResetError: #Excepts the connectionreseterror
print("oops.. something happened")
def send_response(total): #Function to send a respons back to the client
global connection_socket #Global type makes it available for use in functions as a local variable
send = str(total) #Changes the datatype from integer to string
response_server = connection_socket.send(send.encode()) #Sends and encodes the message as byte-array
print("Reponse sent to Client: ",send)
def adder(response): #Function to both determine if the response was integers and to detect errors, will always return total
try:
total = eval(response) #Uses the eval()-function to determine the integers and mathematical operations.
#I had problems with splitting the integers and operators, explained in README.
return total
except (NameError): #If the input is not accepted in the eval()-function. In other words, the request from the server was not integers
total = "Wrong format, could not add"
return total
except (TypeError): #The same as above?
total = "Wrong format, could not add"
return total
except SyntaxError: #The error that will occur if the client is no longer connected, important!
print("Client disconnected from Server!")
return False
def server_function(): #Function to assemble all the other functions #habit from arduino
request = read_request() #Reads the request from the client
total = adder(request) #Checks and add the data from the server
if total == False: ##! If the adder function gets the error that occurs if the client is no longer connected it will return False
return False #If the client is disconnected this function will return False
else:
send_response(total) #Sends the response to the client if it is still connected
return True
if __name__ == "__main__": #trigger
start_server()
while True: #To keep the server check for responses
check = server_function() #Runs the server-function
if check == False:#If the client is not connected the server will break.
print("No contact to Client, closing down the server")
break
print("Goodbye!") #Control print to check if it breaks