more work
This commit is contained in:
parent
dec6af327a
commit
1b25943a2c
5 changed files with 114 additions and 0 deletions
18
app/app.py
18
app/app.py
|
@ -1,4 +1,12 @@
|
|||
from flask import Flask, render_template, send_from_directory
|
||||
import rfc2229
|
||||
import configparser
|
||||
|
||||
# Load configuration
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
server = config['server']['host']
|
||||
port = config['server']['port']
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
@ -6,9 +14,19 @@ app = Flask(__name__)
|
|||
def home():
|
||||
return render_template('index.j2', server="everypizza.im dictd 1.13.3/rf on Linux 6.13.7-arch1-1")
|
||||
|
||||
@app.route('/define/<word>')
|
||||
def define(word):
|
||||
client = rfc2229.RFC2229Client()
|
||||
client.connect(server, port)
|
||||
definition = client.define(word)
|
||||
client.quit()
|
||||
return render_template('define.j2', word=word, definition=definition)
|
||||
|
||||
@app.route('/style.css')
|
||||
def style():
|
||||
return send_from_directory('static', 'output.css')
|
||||
|
||||
print(rfc2229._read_response)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host="0.0.0.0", port=8080)
|
3
app/config.ini
Normal file
3
app/config.ini
Normal file
|
@ -0,0 +1,3 @@
|
|||
[server]
|
||||
host = everypizza.im
|
||||
port = 2628
|
57
app/rfc2229.py
Normal file
57
app/rfc2229.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
import socket
|
||||
|
||||
|
||||
class RFC2229Client:
|
||||
def __init__(self):
|
||||
self.sock = None
|
||||
|
||||
def connect(self, host='dict.org', port=2628):
|
||||
"""Establish a connection to the dictionary server."""
|
||||
self.sock = socket.create_connection((host, port))
|
||||
|
||||
"""Read the response from the server."""
|
||||
response = self.sock.recv(4096).decode('utf-8')
|
||||
print(response)
|
||||
return response
|
||||
|
||||
def define(self, word, database='!'):
|
||||
"""Request a definition for a word and parse the multi-line response."""
|
||||
self._send_command(f"DEFINE {database} {word}")
|
||||
# Use a file-like interface for line-by-line reading.
|
||||
file = self.sock.makefile('r', encoding='utf-8')
|
||||
# Read the initial response header.
|
||||
header = file.readline()
|
||||
response_lines = [header.rstrip('\r\n')]
|
||||
# Read subsequent lines until a single dot indicates the end.
|
||||
for line in file:
|
||||
line = line.rstrip('\r\n')
|
||||
if line == '.':
|
||||
break
|
||||
response_lines.append(line)
|
||||
return "\n".join(response_lines)
|
||||
|
||||
def match(self, word, strategy='.', database='!'):
|
||||
"""Request matches for a word."""
|
||||
self._send_command(f"MATCH {database} {strategy} {word}")
|
||||
return self._read_response()
|
||||
|
||||
def quit(self):
|
||||
"""Close the connection to the server."""
|
||||
self._send_command("QUIT")
|
||||
self.sock.close()
|
||||
self.sock = None
|
||||
|
||||
def _send_command(self, command):
|
||||
"""Send a command to the server."""
|
||||
if not self.sock:
|
||||
raise ConnectionError("Not connected to the server.")
|
||||
self.sock.sendall(f"{command}\r\n".encode('utf-8'))
|
||||
|
||||
if __name__ == "__main__":
|
||||
client = RFC2229Client()
|
||||
try:
|
||||
client.connect()
|
||||
word = input("Enter a word to define: ")
|
||||
print(client.define(word))
|
||||
finally:
|
||||
client.quit()
|
|
@ -34,6 +34,8 @@
|
|||
--text-sm--line-height: calc(1.25 / 0.875);
|
||||
--text-base: 1rem;
|
||||
--text-base--line-height: calc(1.5 / 1);
|
||||
--text-2xl: 1.5rem;
|
||||
--text-2xl--line-height: calc(2 / 1.5);
|
||||
--text-3xl: 1.875rem;
|
||||
--text-3xl--line-height: calc(2.25 / 1.875);
|
||||
--text-4xl: 2.25rem;
|
||||
|
@ -452,6 +454,10 @@
|
|||
.text-center {
|
||||
text-align: center;
|
||||
}
|
||||
.text-2xl {
|
||||
font-size: var(--text-2xl);
|
||||
line-height: var(--tw-leading, var(--text-2xl--line-height));
|
||||
}
|
||||
.text-3xl {
|
||||
font-size: var(--text-3xl);
|
||||
line-height: var(--tw-leading, var(--text-3xl--line-height));
|
||||
|
|
30
app/templates/define.j2
Normal file
30
app/templates/define.j2
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
|
||||
<head>
|
||||
<title>WebDICT</title>
|
||||
<link href="/style.css" rel="stylesheet">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="bg-white dark:bg-gray-950 place-content-center place-items-center min-h-screen">
|
||||
<span class="text-4xl dark:text-white">
|
||||
WebDICT
|
||||
<br />
|
||||
</span>
|
||||
<span class="dark:text-white place-self-center">
|
||||
Word: {{ word }}
|
||||
Definition: {{ definition }}
|
||||
</span>
|
||||
<br />
|
||||
<a href="/">
|
||||
<button type="button"
|
||||
class="text-gray-900 bg-white border border-gray-300 focus:outline-none hover:bg-gray-100 focus:ring-4 focus:ring-gray-100 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-gray-800 dark:text-white dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-600 dark:focus:ring-gray-700 place-self-center">Back
|
||||
home</button>
|
||||
</a>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Add table
Reference in a new issue