Advertisement
ASMProgrammer

Python Server with IP Address (Functionaly)

Jun 15th, 2023
155
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | Software | 0 0
  1. import socket
  2. def start_server():
  3.     try:
  4.         server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5.         server.bind(('124.0.8.23',2957))
  6.         server.listen(100000)
  7.         while True:
  8.             print('Working, please wait...')
  9.             client_socket, address = server.accept()
  10.             data = client_socket.recv(100000).decode('utf-8')
  11.             # print(data)
  12.             content = load_page_request(data)
  13.             client_socket.send(content)
  14.             client_socket.shutdown(socket.SHUT_WR)
  15.     except KeyboardInterrupt:  
  16.         server.close()
  17. def load_page_request(request_file):
  18.     HDRS = 'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnect-RayId: ComputerIPAddress:ComputerPort;\r\n\r\n'
  19.     HDRS_404 = 'HTTP/1.1 404 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnect-RayId: ComputerIPAddress:ComputerPort;\r\n\r\n'
  20.     path = request_file.split(' ')[1]
  21.     response = ''
  22.     try:
  23.         with open('views'+path, 'rb') as file:
  24.             response = file.read()
  25.         return HDRS.encode('utf-8')
  26.     except FileNotFoundError:
  27.         return (HDRS_404 + 'Nginx ERROR: 404 WebSite Not Found').encode('utf-8')
  28. client_socket2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
  29. if __name__ == '__main__':
  30.     start_server()
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement