Installing MySQL for Python 3

sudo apt-get install build-essential python3-dev libmysqlclient-dev

sudo apt-get install python3-pip

**install the MySQL connector
using pip:**

sudo pip3 install mysql-connector

or

sudo apt-get install python3-mysql.connector

demo_mysql_connection.py:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword"
)

print(mydb)

Creating a Database
To create a database in MySQL, use the "CREATE DATABASE" statement:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

Creating a Table

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")

Insert Into Table

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

Select From a Table

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
  print(x)

Python 使用 MySQLdb
檢查是否有安裝 MySQLdb 模組

python -c "import MySQLdb"

ImportError: No module named MySQLdb

MySQLdb 只支持python2,不支持python3

python mysql db 全局变量

global conn 
conn = mysql.connector.connect(
      host="localhost",
      user="yourusername",
      password="yourpassword"
    )

python html parse

from html.parser import HTMLParser


class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Encountered a start tag:", tag, type(tag))
        for attr in attrs:
            # print("     attr:", attr, type(attr))
            if(tag == "a" and attr[0] == 'href'):
                print("href = ", attr[1])


    def handle_endtag(self, tag):
        print("Encountered an end tag :", tag)

    def handle_data(self, data):
        print("Encountered some data  :", data)


parser = MyHTMLParser()
parser.feed('<html><head><title>Test</title></head>'
            '<body><h1>Parse me!</h1><a href="https://www.baidu.com/">baidu</a></body></html>')

python file read

    try:
        f = open(filebat)
    except FileNotFoundError:
        print("file not found :", filebat)
    else:
        strfile = f.read()

python bytes to string

>>> b"abcde"
b'abcde'

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> b"abcde".decode("utf-8") 
'abcde'

python encoding

import os
import sys

ch = '中国'

print('GBK:', ch.encode('gbk'))
print('UTF8:', ch.encode('utf8'))

print('GBK:', ch.encode('gbk').decode('gbk'))
print('UTF8:', ch.encode('utf8').decode('utf8'))

GBK: b'xd6xd0xb9xfa'
UTF8: b'xe4xb8xadxe5x9bxbd'
GBK: 中国
UTF8: 中国

python tcp socket

import socket

socket.setdefaulttimeout(2)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect(("127.0.0.1", 5000))
except Exception as e:
    print( str(e))
    exit(0)

s.send(b'GET / HTTP/1.1\r\n\r\n')
while True:
    data = s.recv(1024)
    if data:
        print(data.decode("utf8"))
    else:
        break

输出:

HTTP/1.0 200 OK

Content-Type: text/html; charset=utf-8
Content-Length: 10
Server: Werkzeug/2.0.3 Python/3.8.10
Date: Wed, 09 Mar 2022 01:46:40 GMT


Index Page

安装bluetooth

sudo apt-get update
sudo apt-get install python-pip python-dev ipython

sudo apt-get install bluetooth libbluetooth-dev
sudo pip install pybluez

python3 requests post octet-stream

import requests
with open('./x.png', 'rb') as f:
    data = f.read()
res = requests.post(url='http://httpbin.org/post',
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})

python 实现sm3计算hash

p1 = subprocess.Popen(["echo",  "-n", str], stdout=subprocess.PIPE)
p2 = subprocess.Popen(["openssl", "dgst", "-sm3"], stdin=p1.stdout, stdout=subprocess.PIPE)
sm3 = p2.stdout.readline()[9:-1]
print('sm3 = ', sm3)

python hex string to string

a = 'aabbccddeeff'
a_bytes = bytes.fromhex(a)
print(a_bytes)
b'\xaa\xbb\xcc\xdd\xee\xff'
aa = a_bytes.hex()
print(aa)
aabbccddeeff

python3 read file line by line

file1 = open('myfile.txt', 'r')
Lines = file1.readlines()
 
count = 0
for line in Lines:
    count += 1
    print("Line{}: {}".format(count, line.strip()))

python3 ubuntu beep

import os
import time
duration = 1  # seconds
freq = 440  # Hz

bool1 = True
elapsed_time = 0
while bool1:
    start = time.time()
    elapsed_time += time.time() - start
    if elapsed_time >= 2.5:#Adjust yourself
        os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq))
        bool1 = False

安装使用
To install it right away for all UNIX users (Linux, OS X, etc.), type:

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl

sudo chmod a+rx /usr/local/bin/youtube-dl

windows.exe地址
https://yt-dl.org/downloads/2021.12.17/youtube-dl.exe

Windows users can download an .exe file and place it in any location on their PATH except for %SYSTEMROOT%System32 (e.g. do not put in C:WindowsSystem32).

If you do not have curl, you can alternatively use a recent wget:

sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl

sudo chmod a+rx /usr/local/bin/youtube-dl

You can also use pip:

sudo pip install --upgrade youtube_dl

签名校验

sudo wget https://yt-dl.org/downloads/latest/youtube-dl.sig -O youtube-dl.sig
gpg --verify youtube-dl.sig /usr/local/bin/youtube-dl
rm youtube-dl.sig

github 地址:
https://github.com/ytdl-org/youtube-dl

youtube-dl 下载慢

youtube-dl    https://www.youtube.com/***   --external-downloader aria2c --external-downloader-args "-x 16  -k 1M"

参数说明

--external-downloader aria2c //调用外部下载工具
--external-downloader-args //外部下载工具指定参数
-x 16 //启用aria2 16个线程,最多就支持16线程
-K 1M //指定块的大小

youtube-dl 下载慢 改用yt-dlp这个工具

安装yt-dlp这个工具得是Python3,2.7不受支持;

安装命令:

pip install yt-dlp

升级命令:

pip install -U yt-dlp

yt-dlp GitHub地址:https://github.com/yt-dlp/yt-dlp

youtube-dl 下载需要登录的资源

安装GetCookie插件(https://chrome.google.com/webstore/detail/get-cookiestxt/bgaddhkoddajcdgocldbbfleckgcbcid

I then visited YouTube.com, while I was logged in, and clicked on the Get
Cookies icon. Then I clicked on the “Export” button to download my YouTube cookies as a text file.

This created a youtube.com_cookies.txt file that I then copied into the YouTube-DL directory on my laptop.
Finally I ran the following command in YouTube-DL to pass the cookie to YouTube and download the video:

youtube-dl -f bestvideo+bestaudio --merge-output-format mkv --all-subs --cookies youtube.com_cookies.txt [YouTube URL]

使用 youtube-dl 只下载音频
如果你只想从 YouTube 视频中下载音频,你可以使用 youtube-dl 的 -x 选项。这个提取音频的选项将视频文件转换为纯音频文件。

youtube-dl -x video_URL

该文件被保存在你运行 youtube-dl 命令的同一目录下。

如你想提取 MP3 格式的音频。你可以像这样使用它:

youtube-dl -x --audio-format mp3 video_URL

使用multipass 安装

sudo snap install multipass

安装ubuntu20.04

multipass launch --name ubuntu

multipass list

Name State IPv4 Image
ubuntu Running 10.16.232.162 Ubuntu 20.04 LTS

进入虚拟机

multipass shell ubuntu
uname -a

Linux ubuntu 5.4.0-99-generic #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

sudo apt update && sudo apt upgrade

Install Java

sudo apt install openjdk-8-jdk

Verify the installation by checking the java version

java -version

openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

Install and configure Elasticsearch

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

Create the repository file using the command

echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-8.x.list
sudo apt update && sudo apt install elasticsearch

注意会生成用户密码,类似这样。

--------------------------- Security autoconfiguration information ------------------------------

Authentication and authorization are enabled.
TLS for the transport and HTTP layers is enabled and configured.

The generated password for the elastic built-in superuser is : wynW6PEAcpowTdai89*F

If this node should join an existing cluster, you can reconfigure this with
'/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <token-here>'
after creating an enrollment token on your existing cluster.

You can complete the following actions at any time:

Reset the password of the elastic built-in superuser with 
'/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.

The default configuration file of elasticsearch is located at /etc/elasticsearch/elasticsearch.yml . Use any text editor and uncomment the lines:

network.host: localhost
http.port: 9200

Start and enable elasticsearch

sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch

Run the following command to view Elasticsearch status and details

curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200

Enter host password for user 'elastic':

{
  "name" : "ubuntu",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "tAw89cXJRmuth97Tm2bxcA",
  "version" : {
    "number" : "8.0.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "1b6a7ece17463df5ff54a3e1302d825889aa1161",
    "build_date" : "2022-02-03T16:47:57.507843096Z",
    "build_snapshot" : false,
    "lucene_version" : "9.0.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

或者:

curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic:wynW6PEAcpowTdai89*F https://localhost:9200
{
  "name" : "ubuntu",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "tAw89cXJRmuth97Tm2bxcA",
  "version" : {
    "number" : "8.0.0",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "1b6a7ece17463df5ff54a3e1302d825889aa1161",
    "build_date" : "2022-02-03T16:47:57.507843096Z",
    "build_snapshot" : false,
    "lucene_version" : "9.0.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

计算集群中文档的数量

curl --cacert /etc/elasticsearch/certs/http_ca.crt -H "Content-Type: application/json"  -u elastic:DCukiJJUAq4u7cEcDyHq  -XGET 'https://localhost:9200/_count?pretty' -d '

返回值:

{
  "count" : 0,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "skipped" : 0,
    "failed" : 0
  }
}

创建第一个索引

curl --cacert /etc/elasticsearch/certs/http_ca.crt -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -X PUT https://localhost:9200/my-index-000001?pretty
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "my-index-000001"
}

查看是否成功创建索引

curl -k -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/_cat/indices?v"

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open my-index-000001 W0g1xmavRciKFTbBljq8Ng 1 1 0 0 225b 225b

添加数据

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -X POST -d'{"@timestamp": "2099-05-06T16:21:15.000Z","event": {"original": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736"}}' "https://localhost:9200/logs-my_app-default/_doc?pretty" 

结果:

{
  "_index" : ".ds-logs-my_app-default-2022.02.28-000001",
  "_id" : "giyiP38BLdok8EvkSQ4j",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

命令

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -X POST -d'{"@timestamp": "2099-05-06T16:21:15.000Z","event": {"original": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736"}}' "https://localhost:9200/my-index-000001/_doc?pretty" 

结果

{
  "_index" : "my-index-000001",
  "_id" : "hCyiP38BLdok8Evkzg7X",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

创建新的索引(index)和类别(type)
Create index命令

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i1"

结果

{"acknowledged":true,"shards_acknowledged":true,"index":"i1"}

命令:

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i2?pretty"

结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "i2"
}

命令:

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i1/_mapping?pretty"

结果:

{
  "i1" : {
    "mappings" : { }
  }
}

ElasticSearch8.0.0已经移除了对type的支持,不再使用type了。
命令

curl -k -u elastic:DCukiJJUAq4u7cEcDyHq -H "Content-Type: application/json" -XPUT "https://localhost:9200/i3?pretty" -d '{"mappings": {"properties": {"field3": {"type": "text"},"field4": {"type": "text"}}}}'

结果

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "i3"
}

命令

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i3/_mapping?pretty"

结果

{
  "i3" : {
    "mappings" : {
      "properties" : {
        "field3" : {
          "type" : "text"
        },
        "field4" : {
          "type" : "text"
        }
      }
    }
  }
}

参考链接:https://www.elastic.co/cn/blog/moving-from-types-to-typeless-apis-in-elasticsearch-7-0
添加记录命令

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i3/_doc/1" -d '{"field3":"3", "field4":"4"}'

结果

{"_index":"i3","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

再次运行命令

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq -XPUT "https://localhost:9200/i3/_doc/1?pretty" -d '{"field3":"3", "field4":"4"}'

结果:

{
  "_index" : "i3",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

查询当前记录命令

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i3/_doc/1?pretty"

结果

{
  "_index" : "i3",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "field3" : "3",
    "field4" : "4"
  }
}

查询命令

curl -k -H "Content-Type: application/json" -u elastic:DCukiJJUAq4u7cEcDyHq "https://localhost:9200/i3/_search?pretty"

结果

{
  "took" : 1008,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "i3",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "field3" : "3",
          "field4" : "4"
        }
      }
    ]
  }
}

sudo qemu-img resize multipass
硬盘扩容

sudo qemu-img resize /var/snap/multipass/common/data/multipassd/vault/instances/ubuntu/ubuntu-20.04-server-cloudimg-amd64.img +5G

Image resized.

安装multipass

sudo snap install multipass

查看版本

multipass version

multipass 1.8.0
multipassd 1.8.0

运行实例

multipass launch --name ubuntu

启动实例

multipass start ubuntu

查看状态

multipass info ubuntu

Name: ubuntu
State: Running
IPv4: 10.16.232.162
Release: Ubuntu 20.04.4 LTS
Image hash: 3d7282d3e92b (Ubuntu 20.04 LTS)
Load: 2.88 2.31 1.01
Disk usage: 4.5G out of 9.5G
Memory usage: 426.9M out of 976.8M
Mounts: --

进入实例

multipass shell ubuntu

添加官方仓库

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/logstash-8.x.list

先安装openjdk-17-jdk

sudo apt install openjdk-17-jdk

安装logstash

sudo apt update && sudo apt install logstash

需要注意的是logstash不支持jdk8
https://www.elastic.co/cn/support/matrix#matrix_jvm