def __init__(self, address, listen_backlog=1021, debug=True, bufferSize=1024, path='./content'): # set vars self.debug = debug self.address = address self.bufferSize = bufferSize self.listen_backlog = listen_backlog self.path = path # define a new socket self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if self.debug == True: eprint('a new socket created') # bind socket to the address self.socket.bind(address) # start listening self.socket.listen(self.listen_backlog) if self.debug == True: eprint( f'started listening at ip: {address[0]}, port: {address[1]} accepting {self.listen_backlog} reqs as max' ) # define a new request handler object for later uses self.handler = request_handler(self.debug, bufferSize=self.bufferSize, path=self.path)
def http_parser(self, request): # TODO: add method to dictionary. # split request into lines lines = request.split('\n') # dictionary to store requests in request_dic = {} # start parsing lines for line in lines: # parse header only. and not message body if len(line) == 0: break # split line into words by space character words = line.split(' ') # if a line is not only one word, if len(words) > 1: # then add it to dictionary. request_dic[words[0]] = words[1:] # this will do like this: # let line is like : word0 word1 word2 ... # so dictionary element will be like this: # word0 : [word1 word2 ...] eprint(request_dic)
def accept(self): # wait for a new connection conn, address = self.socket.accept() if self.debug == True: eprint(f'connected to {address[0]} : {address[1]}') # with new connection, start a new thread of request handler threading.Thread(target=self.handler.handle, args=(conn, ), kwargs={ 'name': address }).start()
def handle(self, conn:socket.socket, name=''): # start communicating try: while True: # wait for receiving new data data = conn.recv(self.bufferSize) # check if connection is going to end if not data: if self.debug == True: eprint(name, ':', '__END__') # end the connection break # now the request is going to get handled self.default_answer(conn, data, name=name) if self.debug == True: eprint(name, ': request is:\n', str(data, encoding='utf-8')) conn.shutdown(1) conn.close() except KeyboardInterrupt: conn.close()
def http_parser(self, request): lines = request.split('\n') for line in lines: if len(line) == 0: break words = line.split(' ') if words[0] == 'GET': link = words[1] if len(words) >= 3: http_version = words[2] elif words[0] == 'Host:': hostname = words[1] elif words[0] == 'User-Agent:': user_agent = words[1:] elif words[0] == 'Accept:': accept_file_types = words[1:] elif words[0] == 'Accept-Language:': accept_language = words[1:] elif words[0] == 'Accept-Encoding:': accept_encoding = words[1:] elif words[0] == 'Connection:': connection_mode = words[1] elif words[0] == 'Cache-Control:': cache_control = words[1] eprint(words[0]) eprint(words) eprint(link, http_version, hostname) print() print() for s in [link, http_version, hostname, user_agent, accept_file_types, accept_language, accept_encoding, connection_mode, cache_control]: eprint(s) print() print()