标签 python 下的文章

“”

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

pyOpenSSL是Python的openssl库.
通过pip安装:

pip install pyOpenSSL

产生密钥对

from OpenSSL.crypto import PKey
from OpenSSL.crypto import TYPE_RSA, FILETYPE_PEM
from OpenSSL.crypto import dump_privatekey, dump_publickey

pk = PKey()
print(pk)
pk.generate_key(TYPE_RSA, 1024)
dpub = dump_publickey(FILETYPE_PEM, pk)
print(dpub)
dpri = dump_privatekey(FILETYPE_PEM, pk)
print(dpri)

运行结果:
<OpenSSL.crypto.PKey object at 0x76c3b090>
b'-----BEGIN PUBLIC KEY-----nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCyNCTZuEzZrX2OaaPgcdCsd3VInPXVGyWKzCc0rUdmmrD7+czdeCgoeHuCwwkig+pGhYFYZvFNZFaEzxKmmJOTxrklBnxOk2K2mTvqsviPMFG780qG69zM+Zm+tYPy+aU4taRoPhlSY9hy2YWubKiLqUkGWXnfoJOElkGFD+O4IwsWwIDAQABn-----END PUBLIC KEY-----n'
b'-----BEGIN PRIVATE KEY-----nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALI0JNm4TNmtfY5pno+Bx0Kx3dUg9dUbJYrMJzStR2aasPv5zN14KCh4e4LDCSKD6kaFgVhm8U1kVoTPEnqaYk5PGuSUHE6TYraZO+qy+I8wUbvzSobr3Mz5mb61g/L5pTi1pGg+GVJj2HLZhan5sqIupSQZZd+gk4SWQYUP47gjCxbAgMBAAECgYEAhqYNvhCayNNlDmlV8O4uvVIZn5TbC2XSrRhq+0t+qtFxr0Llf+Ydec7njDswOMsyBo0z2YcXBuIs2XbZYdXhlH9AgnWWrkhRPVLN0mvs/XPpXRkQh233orznIgoz8UBuoUlcXppA/KOnSSJ2RuwVtiqAl5nQf71oRSVZ7AmKy6PLOECQQDofMyLSx9KWoQ/HSiw7w9lgX7+olyB3ybg+Hi9YdrhnyxWJ2VJoY5TTCCsREsAyF86fRSNuaF3PpU/6oYkT+6brAkEAxDnp8jAKmrfX8qKTnnNOwFJaAJBAihtrgeURPWGiCTSWR9w0S5w+AKeeGU9oEhuPaUK1rUdtqYlFMhAgjn2iYUUQJAEw9wQZdGGHV1VCtS07a1v2+vdrbe+LLP4C/ezkAAjvR0bpnHnNFVOTv5nM+win7i98ubbMckSr9xwwy6NK3s9QwJAU2wjr5kJCRnbrwW7J9M/aqFJPQu3AgoPnoL6P1RApRU8RrSxbuuv2GtqZWxC3F/nKmL4BgD1+DuptUzx6sYW64QJAC5jNhQw7nd1AzDBc4X8fkcOH1+fn3sNGT5UBZ5+1l8jy44QR0ZaDsbGKyWEizDiJVvC01eNJdnTDj99Venyyug6Q==n-----END PRIVATE KEY-----n'

签名与验签

from OpenSSL.crypto import PKey
from OpenSSL.crypto import TYPE_RSA, FILETYPE_PEM
from OpenSSL.crypto import sign, verify
from OpenSSL.crypto import X509

pk = PKey()
pk.generate_key(TYPE_RSA, 1024)
 
signature = sign(pk, 'hello, world!', 'sha256')
print(signature)
 
x509 = X509()
x509.set_pubkey(pk)
verify(x509, signature, 'hello, world!', 'sha256')

结果:
b'txe1xb1rxc1}x82x9dxbexa2x97x14x88xdbxf7x19x835xeb=xc0x87xa5xe9xe7x10xcdxaax90Qx11xee;oxf4Axafxa0xfcj3Xtxd9=x10xf3xbdxe9xc3>@xc1xafxffx8dxfbtxd9x81xfaxdexa2QLxc2xf0t+_wxfex1bx86x0f\xebJ\x17xcaxf4x11xb0lxd6x17`xfdx194xa6x0cxe3yx93Exd2x92Bx984-(xc8qxdax1e:,xd4x83jxca(jxe4xb5Gxa6(xfaxffx97xa2xabxa9xd6'

如果验签失败,会出现以下错误.
OpenSSL.crypto.Error: [('rsa routines', 'int_rsa_verify', 'bad signature')]

安装rsa模块

pip install rsa

RSA加解密过程:

python

Python 3.5.3 (default, Nov 4 2021, 15:29:10)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.

import rsa
(pubkey, privkey) = rsa.newkeys(512)
print(pubkey, privkey)

PublicKey(7923383863263798057086493131602855244106043226226580784778365931183912547588793153219251578468413935121113886104438479941506950504197502414270136297859107, 65537) PrivateKey(7923383863263798057086493131602855244106043226226580784778365931183912547588793153219251578468413935121113886104438479941506950504197502414270136297859107, 65537, 4082772678981620464589634451595715726894137353703581688236651319042384096914189930827359104990339125600702388770355631338209042409912997066322913565755233, 5111290471875396921428642977019854300275057344938560249265410597941457399078028889, 1550172878427042048581175711004305712704687713604245356094500484144351963)

rsa.encrypt("hello".encode('utf-8'), pubkey)

b'x16rx17xbbOxdcQxa0xffxdfjxad]x1axc7x96xbcx94LxcfBx83GOxa9x18Syx94x13xcaxafN_xd2xd25xa9Etxa1xb6lxc9xb1~xc8xc1+x10x9bx90x06xc6xddxb4xeax86x00x13xf8x0bN~'

crypto = rsa.encrypt('hello'.encode('utf-8'), pubkey)
print(crypto)

b'x16b~x81x0bxd9xb8>x1ex0fKxd9KY5xf1nx80x:xc2w/QFxa6!&xd0+q!xf5x14xe7xe9=Nx1dx0cxdd"6x80xa4xabxb8xf5=xcbxb05x07xf4xb0xa6xe9xe4DbXx99x17x8d'

rsa.decrypt(crypto, privkey)

b'hello'