acolyte-py 2 jaren geleden
bovenliggende
commit
41a4c56701
6 gewijzigde bestanden met toevoegingen van 116 en 8 verwijderingen
  1. 8 4
      core/settings.py
  2. 2 0
      main/urls.py
  3. 47 4
      main/views.py
  4. 35 0
      static/style.css
  5. 24 0
      templates/post_form.html
  6. BIN
      отчёт.docx

+ 8 - 4
core/settings.py

@@ -9,7 +9,7 @@ https://docs.djangoproject.com/en/4.1/topics/settings/
 For the full list of settings and their values, see
 https://docs.djangoproject.com/en/4.1/ref/settings/
 """
-
+import os
 from pathlib import Path
 
 # Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -56,7 +56,7 @@ ROOT_URLCONF = 'core.urls'
 TEMPLATES = [
     {
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
-        'DIRS': [],
+        'DIRS': [os.path.join(BASE_DIR), 'templates'],
         'APP_DIRS': True,
         'OPTIONS': {
             'context_processors': [
@@ -117,9 +117,13 @@ USE_TZ = True
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/4.1/howto/static-files/
 
-STATIC_URL = 'static/'
-
 # Default primary key field type
 # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
 
 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
+
+STATIC_URL = '/static/'
+
+STATICFILES_DIRS = [
+   os.path.join(BASE_DIR, 'static'),
+]

+ 2 - 0
main/urls.py

@@ -6,4 +6,6 @@ urlpatterns = [
     path('', views.base),
     path('info', views.base_info),
     path('ip', views.status_output),
+    path('get_ipv4', views.get_status_ipv4),
+    path('post_ipv4', views.post_status_ipv4),
 ]

+ 47 - 4
main/views.py

@@ -1,22 +1,65 @@
 import requests
+import subprocess
 
 from bs4 import BeautifulSoup
 from django.http import HttpResponse
+from django.shortcuts import render
 
 
-def base(request):
+def base(request) -> HttpResponse:
     return HttpResponse("<h1> Base views: Response - 200_OK </h1>")
 
 
-def base_info(request):
+def base_info(request) -> HttpResponse:
     return HttpResponse("<h1> Info views: Response - 200_OK<br>Mironov Misha</h1>")
 
 
-def status_output(request):
+def status_output(request) -> HttpResponse:
+    user_agent = request.META["HTTP_USER_AGENT"]
     response = requests.get(url='https://2ip.ru/')
     bs = BeautifulSoup(response.text, "lxml")
     ip = bs.find('div', 'ip')
 
     return HttpResponse(
-        f"<h1> IP: {ip.text} </h1><br><h3>Для безопасности изменены последние 2 числа (после точек)</h3>"
+        f"<h1> IP: {ip.text} </h1><br><h1> IP: {user_agent} </h1><br><h3>Для безопасности изменены последние 2 "
+        f"числа (после точек)</h3> "
     )
+
+
+def get_status_ipv4(request) -> HttpResponse:
+    get_info = subprocess.check_output(
+        ["powershell.exe", "Get-NetIPConfiguration -InterfaceAlias Ethernet -Detailed"], shell=True
+    )
+    decoded = get_info.decode('ISO-8859-1').replace('\r', '')
+
+    return HttpResponse(
+        f'<h3 style="max-width: 320px;">{decoded}</h3>'
+    )
+
+
+def post_status_ipv4(request):
+    if request.method == 'POST':
+        ip_addr = request.POST.get('ip_addr')
+        ip_mask = request.POST.get('ip_mask')
+        ip_dns = request.POST.get('ip_dns')
+        #  -InterfaceAlias Ethernet - Это моя локальная, личная настройка. Её следует менять для других!
+        if ip_addr:
+            subprocess.call(
+                ["powershell.exe", f"Set-NetIPAddress -InterfaceAlias Ethernet -IPAddress {ip_addr}"],
+                shell=True
+            )
+        if ip_mask:
+            subprocess.call(
+                ["powershell.exe", f"Set-NetIPAddress -InterfaceAlias Ethernet -PrefixLength {ip_mask}"],
+                shell=True
+            )
+        if ip_dns:
+            subprocess.call(
+                ["powershell.exe", f"Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses {ip_dns}"],
+                shell=True
+            )
+        return HttpResponse(
+            "<h1>Good news! Check your ip setting! ( win + R -> ncpa.cpl)</h1>"
+        )
+    else:
+        return render(request, 'post_form.html')

+ 35 - 0
static/style.css

@@ -0,0 +1,35 @@
+input[type=text]:focus {
+  background-color: #e6e6e6;
+  cursor: pointer;
+}
+
+button {
+  background-color: #e6e6e6;
+  border-radius: 5px;
+  border: 1px solid black;
+  color: black;
+  padding: 8px 50px;
+  text-decoration: none;
+  cursor: pointer;
+}
+
+.center__form {
+  display: flex;
+  justify-content: center;
+  padding: 150px;
+}
+
+.h4__form {
+  padding: 0 250px 0 0;
+}
+
+.button__center {
+  display: flex;
+  justify-content: center;
+}
+
+.pq {
+  display: flex;
+  justify-content: space-between;
+  max-width: 215px;
+}

+ 24 - 0
templates/post_form.html

@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <link rel="stylesheet" href="/static/style.css">
+    <meta charset="UTF-8">
+    <title>Post_IPv4</title>
+    <div class="center__form">
+        <form action="" method="post">
+            {% csrf_token %}
+              <fieldset>
+                  <legend>Протокол IPv4</legend>
+                  <h4 class="h4__form">Пример: 192.168.255.255 | 8.8.8.8<br><br>Маска от 0 до 24</h4>
+                  <p class="pq"><label for="ip">IP-адрес </label><input id="ip" type="text" name="ip_addr" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"></p>
+                  <p class="pq"><label for="mask">Маска </label><input id="mask" type="text" name="ip_mask" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"></p>
+                  <p class="pq"><label for="dns">DNS </label><input id="dns" type="text" name="ip_dns" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"></p>
+                  <br><p class="button__center"><button>Изменить</button></p>
+              </fieldset>
+        </form>
+    </div>
+</head>
+<body>
+
+</body>
+</html>

BIN
отчёт.docx