From 8e323c4a98326ce234163b5dc0d6ec130370d0ad Mon Sep 17 00:00:00 2001 From: liming Date: Tue, 23 Mar 2021 01:01:52 +0800 Subject: [PATCH 1/2] bug fix --- text_html/t.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text_html/t.py b/text_html/t.py index 08def26..1d51797 100644 --- a/text_html/t.py +++ b/text_html/t.py @@ -18,7 +18,7 @@ def convert_encoding(data, new_coding='UTF-8'): """未知编码转成utf8""" encoding = cchardet.detect(data)['encoding'] if new_coding.upper() != encoding.upper(): - data = data.decode(encoding, data).encode(new_coding) + data = data.decode(encoding).encode(new_coding) return data From bb1d4e10fb6b3c0da6fd07aa68df320b54da996f Mon Sep 17 00:00:00 2001 From: liming Date: Thu, 15 Apr 2021 23:12:21 +0800 Subject: [PATCH 2/2] get host ip from socket --- socket_programming/get_host_ip.py | 22 ++++++++++++++++++++++ text_html/to_utf8.py | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 socket_programming/get_host_ip.py diff --git a/socket_programming/get_host_ip.py b/socket_programming/get_host_ip.py new file mode 100644 index 0000000..d05d8c8 --- /dev/null +++ b/socket_programming/get_host_ip.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import socket + + +def get_host_ip(): + """ + 查询本机ip地址 + :return: ip + """ + s = None + try: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.connect(('8.8.8.8', 80)) + ip = s.getsockname()[0] + finally: + s.close() + return ip + + +if __name__ == '__main__': + print(get_host_ip()) diff --git a/text_html/to_utf8.py b/text_html/to_utf8.py index a86bf68..e4b86a2 100644 --- a/text_html/to_utf8.py +++ b/text_html/to_utf8.py @@ -32,7 +32,7 @@ def to_utf8(filename): lines[i] = line.replace('charset=gbk', 'charset=utf-8') txt = '\n'.join(lines) - content = txt.decode('gb18030').encode('utf-8') + content = txt.encode('gb18030').decode('utf-8') filename += 'l' with open(filename, 'w', encoding='utf-8') as f: f.write(content)