Make requests in an external module
This commit is contained in:
parent
6e302e6e63
commit
9a9f1172b1
Binary file not shown.
|
@ -0,0 +1,38 @@
|
|||
import socket
|
||||
import ssl
|
||||
import urllib.parse
|
||||
|
||||
# See https://tildegit.org/solderpunk/gemini-demo-1/src/branch/master/gemini-demo.py
|
||||
|
||||
class Request():
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
parsed_url = urllib.parse.urlparse(self.url)
|
||||
if ":" in parsed_url.netloc:
|
||||
self.hostname, self.port = parsed_url.netloc.split(":")
|
||||
else:
|
||||
self.hostname = parsed_url.netloc
|
||||
self.port = 1965
|
||||
|
||||
def makeRequest(self):
|
||||
try:
|
||||
s = socket.create_connection((self.hostname, self.port))
|
||||
context = ssl.SSLContext()
|
||||
context.check_hostname = False
|
||||
context.verify_mode = ssl.CERT_NONE
|
||||
s = context.wrap_socket(s, server_hostname = self.hostname)
|
||||
s.sendall((self.url + '\r\n').encode("UTF-8"))
|
||||
fp = s.makefile("rb")
|
||||
header = fp.readline().decode("UTF-8").strip()
|
||||
status, mime = header.split()
|
||||
|
||||
print("STATUS : " + status)
|
||||
print("MIME : " + mime)
|
||||
|
||||
body = fp.read().decode("UTF-8")
|
||||
|
||||
return body
|
||||
|
||||
except Exception as err:
|
||||
print(err)
|
||||
exit(1)
|
31
main.py
31
main.py
|
@ -1,6 +1,5 @@
|
|||
import tkinter as tk
|
||||
import socket
|
||||
import ssl
|
||||
import gemini
|
||||
|
||||
# See https://tildegit.org/solderpunk/gemini-demo-1/src/branch/master/gemini-demo.py
|
||||
|
||||
|
@ -13,9 +12,10 @@ class Application():
|
|||
self.current_URL = "about:home"
|
||||
|
||||
def updateContent(self):
|
||||
text = makeRequest("rdelaage.ovh", "gemini://rdelaage.ovh")
|
||||
self.content.setContent(text)
|
||||
self.current_URL = self.nav_bar.getURL()
|
||||
r = gemini.Request(self.current_URL)
|
||||
text = r.makeRequest()
|
||||
self.content.setContent(text)
|
||||
|
||||
class NavBar():
|
||||
def __init__(self, parent):
|
||||
|
@ -43,27 +43,4 @@ class Content():
|
|||
def setContent(self, new_content):
|
||||
self.line["text"] = new_content
|
||||
|
||||
def makeRequest(hostname, url):
|
||||
try:
|
||||
s = socket.create_connection((hostname, 1965))
|
||||
context = ssl.SSLContext()
|
||||
context.check_hostname = False
|
||||
context.verify_mode = ssl.CERT_NONE
|
||||
s = context.wrap_socket(s, server_hostname = hostname)
|
||||
s.sendall((url + '\r\n').encode("UTF-8"))
|
||||
fp = s.makefile("rb")
|
||||
header = fp.readline().decode("UTF-8").strip()
|
||||
status, mime = header.split()
|
||||
|
||||
print("STATUS : " + status)
|
||||
print("MIME : " + mime)
|
||||
|
||||
body = fp.read().decode("UTF-8")
|
||||
|
||||
return body
|
||||
|
||||
except Exception as err:
|
||||
print(err)
|
||||
exit(1)
|
||||
|
||||
app = Application()
|
||||
|
|
Reference in New Issue