Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import re
- from datetime import datetime, date
- LISTEN_PORT = 9090
- LISTEN_HOST = "localhost"
- SERVER_IP = "54.71.128.194"
- SERVER_PORT = 92
- def main():
- last_send = datetime(1, 1, 1, 0, 0)
- count = 0
- while True:
- listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- listen_sock.bind((LISTEN_HOST, LISTEN_PORT))
- server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- listen_sock.listen(1)
- print("Listening: " + LISTEN_HOST + ':' + str(LISTEN_PORT))
- client_sock, client_address = listen_sock.accept() # Create new socket with the program
- forward_msg = client_sock.recv(1024).decode() # Get message from the program
- print("Message received from program: " + forward_msg)
- sent = datetime.today()
- print(sent)
- print(last_send)
- print((sent - last_send).total_seconds() / 60)
- if (sent - last_send).total_seconds() / 60 < 1:
- count += 1
- if count >= 5:
- forward_msg = "ERROR#\"No more requests allows! Please wait 1 minute\""
- client_sock.sendall(forward_msg.encode()) # Send message to the program
- client_sock.close() # Close socket with the client
- server_sock.close() # Close socket with the server
- listen_sock.close() # Close the LISTENING socket
- continue
- else:
- count = 0
- last_send = sent
- if "country:France" in forward_msg: # Check if country equals to France
- forward_msg = "ERROR#\"France is banned\""
- client_sock.sendall(forward_msg.encode()) # Send message to the program
- client_sock.close() # Close socket with the client
- server_sock.close() # Close socket with the server
- listen_sock.close() # Close the LISTENING socket
- continue
- server_sock.connect((SERVER_IP, SERVER_PORT)) # Create new socket with the server
- server_sock.sendall(forward_msg.encode()) # Send message to the server
- forward_msg = server_sock.recv(1024).decode() # Get message from the server
- print("Message received from server: " + forward_msg)
- forward_msg = re.sub(r"imageurl:\"(.+?)jpg", r'imageurl:"\1.jpg', forward_msg) # Fix photo
- forward_msg = re.sub(r"SERVERERROR.+?#", r"ERROR#", forward_msg) # Fix error messages
- client_sock.sendall(forward_msg.encode()) # Send message to the program
- client_sock.close() # Close socket with the client
- server_sock.close() # Close socket with the server
- listen_sock.close() # Close the LISTENING socket
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement