SHARE
TWEET

reee

a guest Dec 13th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # TCP Server
  2.  
  3. import socket, threading, re, os.path, sys
  4.  
  5. # Setting up the Socket
  6. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7. server_ip = '0.0.0.0'
  8. server_port = 12345
  9. server.bind((server_ip,server_port))
  10. server.listen(0)
  11.  
  12. # Regex patterns we'll use
  13. pattern = re.compile("GET\s([^\s]+)\sHTTP/1.1") # Proper request regex
  14. jpgPattern = re.compile("([^\s]+)\.jpg") # JPG file regex
  15.  
  16.  
  17. while True:
  18.     client_socket, client_address = server.accept()
  19.     print 'Connection from:', client_address
  20.     data = client_socket.recv(1024)
  21.     if pattern.match(data): # Checks if the user sent a valid request
  22.         split = data.split(" ")
  23.         if split[1] == "/": # Checks if the file is simply / (Opens index)
  24.             print("Index requested")
  25.             client_socket.send("HTTP/1.1 200 OK\nConnection: close\r\n\r\n")
  26.             f = open("files/index.html", 'r')
  27.             l = f.read(1024)
  28.             while (l):
  29.                 client_socket.send(l)
  30.                 l = f.read(1024)
  31.             f.close()
  32.         elif jpgPattern.match(split[1]): # Check if the file is a JPG type
  33.             print("JPG file requested: " + split[1])
  34.             client_socket.send("HTTP/1.1 200 OK\nConnection: close\r\n\r\n")
  35.             try: # Tries opening and sending the file, if file not found, returns 404
  36.                 with open("files" + split[1], 'rb') as f:
  37.                     l = f.read(1024)
  38.                     while (l):
  39.                         client_socket.send(l)
  40.                         l = f.read(1024)
  41.                     f.close()  
  42.             except IOError as e:
  43.                 client_socket.send("HTTP/1.1 404 Not Found\nConnection: close\r\n\r\n")
  44.         elif split[1] == "/redirect": # Redirects if requested
  45.             print("Redirect request")
  46.             client_socket.send("HTTP/1.1 301 Moved Permanently\nConnection: close\nLocation: /result.html\r\n\r\n")
  47.         else:
  48.             print("Regular file requetssed: " + split[1])
  49.             client_socket.send("HTTP/1.1 200 OK\nConnection: close \r\n\r\n")
  50.             try: # Tries opening and sending the file, if file not found, returns 404
  51.                 with open("files" + split[1], 'r') as f:
  52.                     l = f.read(1024)
  53.                     while (l):
  54.                         client_socket.send(l)
  55.                         l = f.read(1024)
  56.                     f.close()
  57.             except IOError as e:
  58.                 client_socket.send("HTTP/1.1 404 Not Found\nConnection: close\r\n\r\n")
  59.     client_socket.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top