Guest User

Untitled

a guest
Nov 6th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. import socket
  2. import sys
  3. import select
  4. import re
  5.  
  6. word_file = open("words_alpha.txt", "r")
  7. words = word_file.read().splitlines()
  8.  
  9. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  10. def find_word(word):
  11.     shift = 1
  12.     while shift < 27:
  13.         tmp_word = ""
  14.         for letter in word:
  15.             new_letter = ord(letter) + shift
  16.             tmp_word += (chr(new_letter) if new_letter <= 122 else chr(new_letter - 26))
  17.         if tmp_word in words:
  18.             shift = 26 - shift
  19.             print(tmp_word, shift)
  20.             return str(shift) + "\n"
  21.         else:
  22.             shift+= 1
  23.     print("Not found", word)
  24.     return "-1"
  25.  
  26. def close_all():
  27.     word_file.close()
  28.     sock.close()
  29.  
  30. sock.connect(("5.101.72.226", 5001))
  31.  
  32. while True:
  33.     msg2 = sock.recv(1024).decode()
  34.     print(msg2)
  35.  
  36.     if msg2.find("Time is up!") != -1 or msg2.find("Incorrect input!") != -1 or msg2.find("Bad answer!") != -1:
  37.         sys.exit()
  38.    
  39.     if msg2.find("Good job!") != -1:
  40.        msg2 = msg2.replace("Good job!", "")
  41.  
  42.  
  43.     msg = msg2[:-4]
  44.     oldmsgParts = msg.split(" ")
  45.     if len(oldmsgParts) < 4:
  46.         continue
  47.     if len(oldmsgParts) > 4:
  48.         oldmsgParts = oldmsgParts[-4:]
  49.     msgParts = []
  50.     for part in oldmsgParts:
  51.         reg = re.compile('[^a-zA-Z ]')
  52.         msgParts.append(reg.sub('', part))
  53.  
  54.     print(msgParts, len(msgParts))
  55.     res = "-1"
  56.     if not len(msgParts[0]) < 5:
  57.         res = find_word(msgParts[0])
  58.     if res == "-1" and not len(msgParts[1]) < 5:
  59.         res = find_word(msgParts[1])
  60.     if res == "-1" and not len(msgParts[2]) < 5:
  61.         res = find_word(msgParts[2])
  62.     print(res == "-1")
  63.     if res == "-1":
  64.         res = find_word(msgParts[3])
  65.     if res == "-1":
  66.         print("too bad")
  67.         sys.exit()
  68.    
  69.     sock.send(res.encode())
  70.    
  71. close_all()
Advertisement
Add Comment
Please, Sign In to add comment