Advertisement
Yonka2019

Untitled

Apr 23rd, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. import socket
  2. import re
  3. from datetime import datetime, date
  4.  
  5. LISTEN_PORT = 9090
  6. LISTEN_HOST = "localhost"
  7.  
  8. SERVER_IP = "54.71.128.194"
  9. SERVER_PORT = 92
  10.  
  11.  
  12. def main():
  13.     last_send = datetime(1, 1, 1, 0, 0)
  14.     count = 0
  15.     while True:
  16.         listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17.         listen_sock.bind((LISTEN_HOST, LISTEN_PORT))
  18.         server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19.  
  20.         listen_sock.listen(1)
  21.         print("Listening: " + LISTEN_HOST + ':' + str(LISTEN_PORT))
  22.  
  23.         client_sock, client_address = listen_sock.accept()  # Create new socket with the program
  24.  
  25.         forward_msg = client_sock.recv(1024).decode()  # Get message from the program
  26.         print("Message received from program: " + forward_msg)
  27.  
  28.         sent = datetime.today()
  29.         print(sent)
  30.         print(last_send)
  31.         print((sent - last_send).total_seconds() / 60)
  32.         if (sent - last_send).total_seconds() / 60 < 1:
  33.             count += 1
  34.             if count >= 5:
  35.                 forward_msg = "ERROR#\"No more requests allows! Please wait 1 minute\""
  36.                 client_sock.sendall(forward_msg.encode())  # Send message to the program
  37.                
  38.                 client_sock.close()  # Close socket with the client
  39.                 server_sock.close()  # Close socket with the server
  40.                 listen_sock.close()  # Close the LISTENING socket
  41.                 continue
  42.         else:
  43.             count = 0
  44.  
  45.         last_send = sent
  46.  
  47.         if "country:France" in forward_msg:  # Check if country equals to France
  48.             forward_msg = "ERROR#\"France is banned\""
  49.             client_sock.sendall(forward_msg.encode())  # Send message to the program
  50.  
  51.             client_sock.close()  # Close socket with the client
  52.             server_sock.close()  # Close socket with the server
  53.             listen_sock.close()  # Close the LISTENING socket
  54.             continue
  55.  
  56.         server_sock.connect((SERVER_IP, SERVER_PORT))  # Create new socket with the server
  57.  
  58.         server_sock.sendall(forward_msg.encode())  # Send message to the server
  59.  
  60.         forward_msg = server_sock.recv(1024).decode()  # Get message from the server
  61.         print("Message received from server: " + forward_msg)
  62.  
  63.         forward_msg = re.sub(r"imageurl:\"(.+?)jpg", r'imageurl:"\1.jpg', forward_msg)  # Fix photo
  64.         forward_msg = re.sub(r"SERVERERROR.+?#", r"ERROR#", forward_msg)  # Fix error messages
  65.  
  66.         client_sock.sendall(forward_msg.encode())  # Send message to the program
  67.  
  68.         client_sock.close()  # Close socket with the client
  69.         server_sock.close()  # Close socket with the server
  70.         listen_sock.close()  # Close the LISTENING socket
  71.  
  72.  
  73. if __name__ == '__main__':
  74.     main()
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement