veltrixcode commited on
Commit
7a4ac82
·
verified ·
1 Parent(s): a12437b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -3,31 +3,48 @@ import socketserver
3
  import uuid
4
  import json
5
  import http.client
 
6
 
7
  PORT = 7860
8
 
9
- HTML = """
 
 
 
 
 
 
 
 
 
 
 
 
10
  <!DOCTYPE html>
11
  <html>
 
 
 
12
  <body>
13
  <h3>ImgBB Anonymous Upload (Python)</h3>
 
 
 
14
  <input type="file" id="f">
15
  <button onclick="up()">Upload</button>
16
  <pre id="o"></pre>
17
 
18
  <script>
19
- async function up() {
20
  const f = document.getElementById('f').files[0];
21
  if (!f) return alert('Pick file');
22
-
23
  const fd = new FormData();
24
  fd.append('image', f);
25
-
26
- const r = await fetch('/upload', { method: 'POST', body: fd });
27
  const j = await r.json();
28
  document.getElementById('o').textContent =
29
  JSON.stringify(j, null, 2);
30
- }
31
  </script>
32
  </body>
33
  </html>
@@ -116,12 +133,14 @@ class Handler(http.server.BaseHTTPRequestHandler):
116
  "delete_url": image.get("delete_url"),
117
  "width": image.get("display_width"),
118
  "height": image.get("display_height"),
119
- "size": image.get("size")
 
120
  }
121
  else:
122
  output = {
123
  "success": False,
124
- "error": imgbb.get("status_txt", "Upload failed")
 
125
  }
126
 
127
  self.send_response(200)
@@ -130,5 +149,6 @@ class Handler(http.server.BaseHTTPRequestHandler):
130
  self.wfile.write(json.dumps(output).encode())
131
 
132
  with socketserver.TCPServer(("", PORT), Handler) as server:
 
133
  print(f"Running → http://localhost:{PORT}")
134
  server.serve_forever()
 
3
  import uuid
4
  import json
5
  import http.client
6
+ import urllib.request
7
 
8
  PORT = 7860
9
 
10
+ # -------- Detect server region ONCE --------
11
+ def get_country():
12
+ try:
13
+ return urllib.request.urlopen(
14
+ "https://ipinfo.io/country",
15
+ timeout=5
16
+ ).read().decode().strip()
17
+ except Exception:
18
+ return "UNKNOWN"
19
+
20
+ SERVER_COUNTRY = get_country()
21
+
22
+ HTML = f"""
23
  <!DOCTYPE html>
24
  <html>
25
+ <head>
26
+ <title>ImgBB Upload</title>
27
+ </head>
28
  <body>
29
  <h3>ImgBB Anonymous Upload (Python)</h3>
30
+ <p><b>Server Region:</b> {SERVER_COUNTRY}</p>
31
+ <hr>
32
+
33
  <input type="file" id="f">
34
  <button onclick="up()">Upload</button>
35
  <pre id="o"></pre>
36
 
37
  <script>
38
+ async function up() {{
39
  const f = document.getElementById('f').files[0];
40
  if (!f) return alert('Pick file');
 
41
  const fd = new FormData();
42
  fd.append('image', f);
43
+ const r = await fetch('/upload', {{ method: 'POST', body: fd }});
 
44
  const j = await r.json();
45
  document.getElementById('o').textContent =
46
  JSON.stringify(j, null, 2);
47
+ }}
48
  </script>
49
  </body>
50
  </html>
 
133
  "delete_url": image.get("delete_url"),
134
  "width": image.get("display_width"),
135
  "height": image.get("display_height"),
136
+ "size": image.get("size"),
137
+ "server_country": SERVER_COUNTRY
138
  }
139
  else:
140
  output = {
141
  "success": False,
142
+ "error": imgbb.get("status_txt", "Upload failed"),
143
+ "server_country": SERVER_COUNTRY
144
  }
145
 
146
  self.send_response(200)
 
149
  self.wfile.write(json.dumps(output).encode())
150
 
151
  with socketserver.TCPServer(("", PORT), Handler) as server:
152
+ print(f"🌍 Server region: {SERVER_COUNTRY}")
153
  print(f"Running → http://localhost:{PORT}")
154
  server.serve_forever()