initial commit
This commit is contained in:
commit
0e9f0aae85
|
@ -0,0 +1,72 @@
|
|||
import sys
|
||||
import socket
|
||||
|
||||
PORT = 1900
|
||||
|
||||
|
||||
class Session:
|
||||
def __init__(self):
|
||||
self.history = [Page('home')]
|
||||
self.current_page = 0
|
||||
|
||||
def back(self):
|
||||
if self.current_page > 0:
|
||||
self.current_page -= 1
|
||||
self.render()
|
||||
|
||||
def forward(self):
|
||||
if self.current_page < len(self.history) - 1:
|
||||
self.current_page += 1
|
||||
self.render()
|
||||
|
||||
def load(self, url):
|
||||
self.current_page += 1
|
||||
if self.current_page >= len(self.history):
|
||||
self.history.append(Page(url))
|
||||
else:
|
||||
self.history[self.current_page] = Page(url)
|
||||
self.history[self.current_page].request()
|
||||
|
||||
def render(self):
|
||||
print(self.history[self.current_page].content)
|
||||
|
||||
|
||||
class Page:
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
self.host, self.selector = parse_url(url)
|
||||
self.content = ''
|
||||
|
||||
def request(self):
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.connect((self.host, PORT))
|
||||
selector = self.selector + '\r\n'
|
||||
sock.sendall(selector.encode('ascii'))
|
||||
buf = bytearray()
|
||||
while True:
|
||||
data = sock.recv(4096)
|
||||
if len(data) == 0:
|
||||
break
|
||||
buf.extend(data)
|
||||
self.content = buf.decode('ascii')
|
||||
|
||||
|
||||
def parse_url(url: str) -> (str, str):
|
||||
clean_url = url.replace('nex://', '')
|
||||
split_url = clean_url.split('/', 1)
|
||||
if len(split_url) == 1:
|
||||
return clean_url, ''
|
||||
return split_url[0], split_url[1]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
session = Session()
|
||||
while True:
|
||||
url = input('Enter a URL')
|
||||
session.load(url)
|
||||
session.render()
|
||||
command = input('Go [b]ack or [f]orward')
|
||||
if command == 'b':
|
||||
session.back()
|
||||
elif command == 'f':
|
||||
session.forward()
|
Loading…
Reference in New Issue