-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_server.py
638 lines (584 loc) · 33.6 KB
/
http_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# @Author : https://www.bajins.com
# @Description : 来源于see
# https://github.com/freelamb/simple_http_server
# https://github.com/xinghaixu/web_server
# https://github.com/smilejay/python/blob/master/py2016/SimpleHTTPServerWithUpload.py
# https://github.com/Tallguy297/SimpleHTTPServerWithUpload
# https://github.com/rhmoult/SecurityTools/blob/master/Platform_Independent/Python/httpsWithUpload/src/httpsWithUpload.py
# https://github.com/wonjohnchoi/Simple-Python-File-Server-With-Browse-Upload-and-Authentication
# https://github.com/4d4c/http.server_upload
# @see https://gist.github.com/UniIsland/3346170
# @File : http_server.py
# @Version: 1.1.0
# @Time : 2021/01/29 11:25:38
# @Project: scripts_python
# @Package:
# @Software: PyCharm
__version__ = "0.1"
__all__ = ["SimpleHTTPRequestHandler"]
import os, sys
import os.path, time
import posixpath
import http.server
import socketserver
import urllib.request, urllib.parse, urllib.error
import html
import shutil
import mimetypes
import os
import platform
import posixpath
import re
import argparse
import base64
from pip._vendor import chardet
try:
import numpy as np
except:
os.system('pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy ')
import numpy as np
try:
import qrcode
except:
os.system('pip install -i https://pypi.tuna.tsinghua.edu.cn/simple qrcode ')
import qrcode
try:
import PIL
except:
os.system('pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow ')
try:
from io import StringIO
except ImportError:
from io import StringIO
def fbytes(B):
B = float(B)
KB = float(1024)
MB = float(KB ** 2) # 1,048,576
GB = float(KB ** 3) # 1,073,741,824
TB = float(KB ** 4) # 1,099,511,627,776
if B < KB:
return '{0} {1}'.format(B, 'Bytes' if 0 == B > 1 else 'Byte')
elif KB <= B < MB:
return '{0:.2f} KB'.format(B / KB)
elif MB <= B < GB:
return '{0:.2f} MB'.format(B / MB)
elif GB <= B < TB:
return '{0:.2f} GB'.format(B / GB)
elif TB <= B:
return '{0:.2f} TB'.format(B / TB)
def sizeof_fmt(num):
for x in ['bytes', 'KB', 'MB', 'GB']:
if num < 1024.0:
return "%3.1f%s" % (num, x)
num /= 1024.0
return "%3.1f%s" % (num, 'TB')
def modification_date(filename):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(filename)))
class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
"""Simple HTTP request handler with GET/HEAD/POST commands.
This serves files from the current directory and any of its
subdirectories. The MIME type for files is determined by
calling the .guess_type() method. And can reveive file uploaded
by client.
The GET/HEAD/POST requests are identical except that the HEAD
request omits the actual contents of the file.
"""
server_version = "SimpleHTTPWithUpload/" + __version__
def do_GET(self):
"""Serve a GET request."""
f = self.send_head()
encoding = f.encoding
if encoding is None:
encoding = "utf-8"
# print(chardet.detect(f.read())['encoding'])
if f:
for i in f.readlines():
if isinstance(i, str):
self.wfile.write(i.encode(encoding))
else:
self.wfile.write(i)
# shutil.copyfileobj(f, self.wfile)
# self.wfile.writelines(f.readlines())
f.flush()
f.close()
def do_HEAD(self):
"""Serve a HEAD request."""
f = self.send_head()
if f:
f.close()
def do_POST(self):
"""Serve a POST request."""
r, info = self.deal_post_data()
f = StringIO()
if r:
stat = "<strong>Success:</strong>"
else:
stat = "<strong>Failed:</strong>"
f.write(f"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta name="viewport" content="width=device-width" charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Upload Result Page</title>
<style type="text/css">
* {{font-family: Helvetica; font-size: 16px; }}
a {{ text-decoration: none; }}
</style>
</head>
<body>
<h2>Upload Result Page</h2>
<hr>
{stat}
{info}
<br>
<br>
<a href="{self.headers['referer']}"><button>Back</button></a>
<hr>
<small>Powered By: bones7456<br>Check new version
<a href="https://gist.github.com/UniIsland/3346170" target="_blank">here</a>.
</small>
</body>
</html>""")
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(f.tell()))
f.seek(0)
self.end_headers()
if f:
for i in f.readlines():
self.wfile.write(i.encode("utf-8"))
# shutil.copyfileobj(f, self.wfile)
f.close()
def deal_post_data(self):
content_type = self.headers['content-type']
if not content_type:
return False, "Content-Type header doesn't contain boundary"
content_type = content_type.split("=")[1]
remain_bytes = int(self.headers['content-length'])
content_disposition = self.headers['Content-Disposition']
if content_disposition:
content_disposition = content_disposition.encode('utf-8')
path = str(self.translate_path(self.path)).encode('utf-8')
fn = None
out = None
uploaded_files = []
line = self.rfile.readline()
remain_bytes -= len(line)
while remain_bytes > 0:
if b"------WebKitFormBoundary" in line: # 可能有多个文件,按Content-Type分割
if out:
out.flush()
out.close()
fn = None
out = None
if b"Content-Disposition" in line and content_disposition != line:
content_disposition = line # 获取当前请求在二进制流中的header
if fn is None and content_disposition:
fn = re.findall(r'Content-Disposition.*name="file"; filename="(.*)"'.encode('utf-8'),
content_disposition)
if fn is None:
return False, "Can't find out file name..."
else:
fn = fn[0]
if out is None and fn:
try:
if platform.system() == "Linux":
fn = os.path.join(path, fn.decode('gbk').encode('utf-8'))
else:
fn = os.path.join(path, fn)
except Exception as e:
return False, f"""文件名请不要用中文,或者使用IE上传中文名的文件。{e}"""
while os.path.exists(fn): # 如果文件已经存在,则重命名
r = fn.rfind(b".")
if r != -1:
fn = fn[:r] + (time.strftime("%Y%m%d%H%M%S", time.localtime())).encode("utf-8") + fn[r:]
r = fn.rfind(b"\\")
if r == -1:
r = 0
else:
r += 1
uploaded_files.append(fn.decode()[r:])
try:
out = open(fn, 'wb')
except IOError:
return False, "Can't create file to write, do you have permission to write?"
if out:
# 把二进制流写入文件
out.write(line)
line = self.rfile.readline()
remain_bytes -= len(line)
info_list = ""
for i in range(len(uploaded_files)):
info_list += "<br>" + uploaded_files[i]
return True, f"""<br><br>{info_list}<br>"""
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
to the outputfile by the caller unless the command was HEAD,
and must be closed by the caller under all circumstances), or
None, in which case the caller has nothing further to do.
"""
path = self.translate_path(self.path)
if os.path.isdir(path):
if not self.path.endswith('/'):
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path)
content_type = self.guess_type(path)
try:
if "text/plain" not in content_type:
f = open(path, 'rb')
content_type += f";charset={chardet.detect(f.read())['encoding']}"
else:
f = open(path, 'r', encoding="utf-8")
content_type += f";charset=utf-8"
except IOError:
self.send_error(404, "File not found")
return None
self.send_response(200)
self.send_header("Content-type", content_type)
fs = os.fstat(f.fileno())
fs_len = fs[6]
if "text/plain" not in content_type:
self.send_header("Content-Length", str(fs_len))
else:
# 如果不指定Content-Range会报错误net::ERR_CONTENT_LENGTH_MISMATCH:
# 浏览器认为Content-Length只是当前Range的大小,不是整个文件的大小
self.send_header("Content-Range", f"bytes 0-{str(fs_len)}/{str(fs_len)}")
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
return f
def list_directory(self, path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
try:
list = os.listdir(path)
except os.error:
self.send_error(404, "No permission to list directory")
return None
enc = sys.getfilesystemencoding()
list.sort(key=lambda a: a.lower())
f = StringIO()
display_path = html.escape(urllib.parse.unquote(self.path))
f.write(f"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta name="viewport" content="width=device-width" charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0,
maximum-scale=1.0, user-scalable=no">
<meta http-equiv="Content-Type" content="text/html; charset={enc}">
<title>Directory listing for {display_path}</title>
<style type="text/css">
* {{font-family: Helvetica; font-size: 16px; }}
a {{ text-decoration: none; }}
a:link {{ text-decoration: none; font-weight: bold; color: #0000ff; }}
a:visited {{ text-decoration: none; font-weight: bold; color: #0000ff; }}
a:active {{ text-decoration: none; font-weight: bold; color: #0000ff; }}
a:hover {{ text-decoration: none; font-weight: bold; color: #ff0000; }}
table {{border-collapse: separate;}}
th, td {{padding:0px 10px;}}
</style>
</head>
<body>
<h2><input type="button" value="主目录" onClick="location='/'">Directory listing for {display_path}</h2>
<hr>
<form ENCTYPE="multipart/form-data" method="post">
<input name="file" type="file" multiple/>
<input type="submit" value="上传"/>
</form>
<hr>
<table>
<tr>
<td>
<img src="data:image/gif;base64,R0lGODlhGAAYAMIAAP///7+/v7u7u1ZWVTc3NwAAAAAAAAAAACH+RFRoaXMgaWNvbiB
pcyBpbiB0aGUgcHVibGljIGRvbWFpbi4gMTk5NSBLZXZpbiBIdWdoZXMsIGtldmluaEBlaXQuY29tACH5BAEAAAEALAAAAAAYABg
AAANKGLrc/jBKNgIhM4rLcaZWd33KJnJkdaKZuXqTugYFeSpFTVpLnj86oM/n+DWGyCAuyUQymlDiMtrsUavP6xCizUB3NCW4Ny6
bJwkAOw==" alt="[PARENTDIR]" width="24" height="24">
</td>
<td>
<a href="../" >Parent Directory</a>
</td>
</tr>""")
for name in list:
dir_image = 'data:image/gif;base64,R0lGODlhGAAYAMIAAP///7+/v7u7u1ZWVTc3NwAAAAAAAAAAACH' \
'+RFRoaXMgaWNvbiBpcyBpbiB0aGUgcHVibGljIGRvbWFpbi4gMTk5NSBLZXZpbiBIdWdoZXMsIGtldmluaEBla' \
'XQuY29tACH5BAEAAAEALAAAAAAYABgAAANdGLrc/jAuQaulQwYBuv9cFnFfSYoPWXoq2qgrALsTYN+4QOg6veFAG2' \
'FIdMCCNgvBiAxWlq8mUseUBqGMoxWArW1xXYXWGv59b+WxNH1GV9vsNvd9jsMhxLw+70gAADs= '
fullname = os.path.join(path, name)
display_name = link_name = name
fsize = fbytes(os.path.getsize(fullname))
created_date = time.ctime(os.path.getctime(fullname))
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
dir_image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAABmJLR0QA/wD/AP' \
'+gvaeTAAAFlUlEQVR4nO3dfUxVZRwH8O9z30AmBF6ncUEul8gmV0ylzLes1h' \
'+9OedqUVvMps2XBbVsc7U1ajOrlZvTrdocWzS3spVlI2C6stxCVnOo1b3TrhiHlwkIAsIFr' \
'/ft6Y9b6BWZ57n3nMMD/D7/MA7nPOe3++P3POd5DpwDEEIIIYQQQgghhBBCCCFEYyzZBgoLC1M4T50TCjGrFgHdjt' \
'XKQ4wFLjU3N18z4nxGSygheXnFWcwc3Q6gFMA92oakDgfOg7NDYVtwz0Wfr3ciYtCDcEKcTvcjMOFrALMBwGazwD4' \
'rDVarWfPgbiUS4bjc50cgEP5/0wAYK2v9x1NnSAA6E0rIPFfRgyawnwDYVq8owCvb1uC+pXkwmZLu+YRwzuE924W' \
'q6kbU1HsAIMI41iqK94ihgehA9SfpcJSkWVMDPnDkbCxbhso3HwdjxibiVg4cPIl3dtUDQC/j1rsV5czARMeUDNX9' \
'TJbd/jIDShcvysUne541vCrGc29xDtra+3DOdymNmSKDA/09DRMdUzIsqvdkeBoAtmxaOSYZ53zd+LSqAY2/teBy' \
'37AmgTnnZeH4kVcBABWvH8LJU21YvcKF8i1rUOCyx+27eeMqHP7hL0Q5Ww/gA00CmCCqf82dLncPgNlNDTswKytt' \
'dHvdUS/e3lmPbZtXYe0TC5E9N0OPONHZPYiaOg+qqhvx4a51ePSh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UIYuA8Amk63AwBalNg7VBx32vSIa9JTOkZfANQncpzqhETBfgSAbw6fQWfXIGqPeAEAS9wzRc43LSgd1/' \
'DW+22xbziOihyregBwu902/wj+xAS94mhSYryXRaP3K8o5Re0hqgeAnp6eiH3WnBrOkA8gDwD1VePrB8f3jEefF' \
'0kGIYQQQgghhBBCCCGEEEJ08S96MLERXBz0BQAAAABJRU5ErkJggg== '
display_name = name + "/"
link_name = name + "/"
fsize = ''
created_date = ''
if os.path.islink(fullname):
dir_image = 'data:image/gif;base64,R0lGODlhGAAYAPf/AJaWlpqampubm5ycnJ2dnZ6enp+fn6CgoKGhoaKioqOjo6SkpKW' \
'lpaampqioqKmpqaqqqqurq6ysrK2tra6urq+vr7CwsLGxsbKysrOzs7S0tLW1tba2tre3t7i4uLm5ubq6uru7u7y' \
'8vL29vb6+vr+/v8LCwsPDw8bGxtDQ0NTU1NXV1dbW1tfX19jY2Nra2tzc3N3d3eDg4OHh4eLi4uPj4+Tk5OXl5e' \
'fn5+np6erq6uvr6+zs7O7u7u/v7/Dw8PHx8fLy8vPz8/T09PX19fb29vf39/j4+Pr6+vv7+/39/f7+/v///' \
'wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAACwAAAAAGAAYAAAI/wCZCBxIsKDBgwgLrsigwUXChEVGYNBwIYKIJA8LFunwocKGDA' \
'8ieMg4kAiHDxRmCGyhIAEKkhtR2iCYYYEAkiNQ3ijYIQGAjDkuVFBJsIcBAhcyttCgoSCQBQcUFMn44gIFEiwE/' \
'oAqIAfJIREeQLDAZIeCAwO8IuQRowYSIxQgBFhAoQBatQaFiLCQoQIFCxEMREUwoAEPhEA0dMQwQSwCIEFYpKCR8' \
'IfiCjWYgJCr4AhJyx13CFRhQYECGBmRcKwgmmAEBCsyltBQQUfBGwUG4MjoYMOIgjsSIJBAskGGEAR3IEhw4AdJEx' \
'IeyBCIY/kBHySZLNEwgcGGDQYQNBbPLpAIBgULEhB4AIQ8wRMFBIhQ4j4gADs='
display_name = name + "@"
if name.endswith(('.bmp', '.gif', '.jpg', '.png')):
dir_image = name
if name.endswith(('.avi', '.mpg')):
dir_image = 'data:image/gif;base64,R0lGODlhGAAYAMIAAP///7+/v7u7u1ZWVTc3NwAAAAAAAAAAACH' \
'+RFRoaXMgaWNvbiBpcyBpbiB0aGUgcHVibGljIGRvbWFpbi4gMTk5NSBLZXZpbiBIdWdoZXMsIGtldmluaEBlaXQu' \
'Y29tACH5BAEAAAEALAAAAAAYABgAAANvGLrc/jAuQqu99BEh8OXE4GzdYJ4mQIZjNXAwp7oj+MbyKjY6ntstyg03' \
'E9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sVIX9fcO3/r5t1+sX6/rb9bHP//RT+P4EP38zZ14Wt9QxY7O1/hxCx' \
'UgdwWFK+nZgiLPyaYK3KDA9XJsmc6AYt1Rv7qIr9qXr/rA325Ukm8EAEiy4/t3f/TuEPX3W21dZTuq2BDXMJD+D' \
'y7ouZLCjQH0C4y1aNVCUILG5YM3fWb2/BX2DQew1r77xVv3dcSH21gf8CFODtecOJx15JJhrYs2tyLWqsnsWVsN' \
'33nw5i888dN4/s/tZ2n/C+cR4IqwA3arAAAAAElFTkSuQmCC '
f.write(f"""<tr>
<td>
<img src="{dir_image}" width="24" height="24">
</td>
<td>
<a href="{urllib.parse.quote(link_name)}">{html.escape(display_name)}</a>
</td>
<td style="text-align:right; font-weight: bold; color:#FF0000">{fsize}</td>
<td style="text-align:right; font-weight: bold;">{created_date}</td>
</tr>""")
f.write("</table></body></html>")
self.send_response(200)
self.send_header("Content-type", "text/html")
# self.send_header("Content-Length", str(f.tell() + 6))
# 如果不指定Content-Range会报错误net::ERR_CONTENT_LENGTH_MISMATCH:
# 浏览器认为Content-Length只是当前Range的大小,不是整个文件的大小
self.send_header("Content-Range", f"bytes 0-{str(f.tell() + 6)}/{str(f.tell() + 6)}")
self.end_headers()
f.seek(0)
return f
def translate_path(self, path):
"""Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
path = path.split('?', 1)[0]
path = path.split('#', 1)[0]
path = posixpath.normpath(urllib.parse.unquote(path))
words = path.split('/')
words = [_f for _f in words if _f]
path = os.getcwd()
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
def guess_type(self, path):
"""Guess the type of a file.
Argument is a PATH (a filename).
Return value is a string of the form type/subtype,
usable for a MIME Content-type header.
The default implementation looks the file's extension
up in the table self.extensions_map, using application/octet-stream
as a default; however it would be permissible (if
slow) to look inside the data to make a better guess.
"""
base, ext = posixpath.splitext(path)
if ext in self.extensions_map:
return self.extensions_map[ext]
ext = ext.lower()
if ext in self.extensions_map:
return self.extensions_map[ext]
else:
return self.extensions_map['']
if not mimetypes.inited:
mimetypes.init() # try to read system mime.types
extensions_map = mimetypes.types_map.copy()
extensions_map.update({
'': 'application/octet-stream', # Default
'.py': 'text/plain',
'.c': 'text/plain',
'.h': 'text/plain',
})
class ThreadingServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
pass
def getip():
match_ip_dict = {}
ipconfig_result_list = os.popen('ipconfig').readlines()
for i in range(0, len(ipconfig_result_list)):
if re.search(r'IPv4 地址', ipconfig_result_list[i]) is not None:
match_ip = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', ipconfig_result_list[i]).group(0)
for j in range(3, 7):
if re.search(r"无线局域网适配器", ipconfig_result_list[i - j]) is not None:
match_ip_dict[ipconfig_result_list[i - j]] = match_ip
ip_dict = match_ip_dict
for i in ip_dict:
return ip_dict[i]
def showTips(bind, port):
return port
if __name__ == '__main__':
# http.server.test(SimpleHTTPRequestHandler, http.server.HTTPServer)
parser = argparse.ArgumentParser()
parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
help='Specify alternate bind address '
'[default: all interfaces]')
parser.add_argument('port', action='store',
default=8000, type=int,
nargs='?',
help='Specify alternate port [default: 8000]')
args = parser.parse_args()
print('----------------------------------------------------------------------->> ')
port = args.port
if not 1024 < port < 65535:
port = 8080
print('-------->> 现在,在监听 ' + str(port) + ' 端口 ...')
with socketserver.TCPServer((args.bind, port), SimpleHTTPRequestHandler) as httpd:
data = 'http://127.0.0.1:' + str(port)
if platform.system() == "Windows":
data = 'http://' + getip() + ':' + str(port)
print(f'-------->> 您可以访问地址URL:{data}')
print('-------->> 或扫描此二维码: ')
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=1,
border=1
)
# from MyQR import myqr
# myqr.run(
# # 在命令后输入链接或者句子作为参数,然后在程序的当前目录中产生相应的二维码图片文件,默认命名为“qrcode.png”
# words='helloworld',
# version=1, # 设置容错率为最高默认边长是取决于你输入的信息的长度和使用的纠错等级;而默认纠错等级是最高级的H
# level='H', # 控制纠错水平,范围是L、M、Q、H,从左到右依次升高
# picture='test.gif', # 用来将QR码图像与一张同目录下的图片相结合,产生一张黑白图片,格式可以是.jpg, .png, .bmp, .gif
# colorized=True, # 可以使产生的图片由黑白(False)变为彩色(True)的
# contrast=1.0, # 用以调节图片的对比度,1.0 表示原始图片,更小的值表示更低对比度,更大反之。默认为1.0。
# brightness=1.0, # 用来调节图片的亮度
# )
qr.add_data(data)
qr.make(fit=True)
qr.print_ascii(out=None, tty=False, invert=False)
print('----------------------------------------------------------------------->> ')
httpd.serve_forever() # 开始监听