分类 about python 下的文章

from flask import Flask, request

app = Flask(__name__)

@app.route('/submit-form', methods=['POST'])
def submit_form():

# 获取表单数据
data = request.form.get('input_data')

# 判断输入是否为空或长度为 0
if not data or len(data.strip()) == 0:
    return "输入不能为空"

# 判断输入长度是否超过限制
if len(data.strip()) > 100:
    return "输入长度不能超过 100 个字符"

# 对输入进行处理或执行其他操作
# ...

return "提交成功"

if name == '__main__':

app.run(debug=True)

# -*- coding:utf-8 -*-
import random

def generate_key(length):
    """生成给定长度的随机字节序列"""
    key = bytearray(length)
    for i in range(length):
        key[i] = random.randint(0, 255)
    return key

def xor_encrypt(plaintext, key):
    """对给定字符串进行异或加密"""
    ciphertext = bytearray(len(plaintext))
    for i in range(len(plaintext)):
        ciphertext[i] = plaintext[i] ^ key[i % len(key)]
    return ciphertext

# 生成一个长度为16的随机密钥
key = generate_key(16)
# print(key)
# print(key.hex())
# 要加密的字符串
plaintext = "比亚迪元EV360电池容量是42KWH(42度电),但充满需要5比亚迪元EV360电池容量是42KWH(42度电),但充满需要5比亚迪元EV360电池容量是42KWH(42度电),但充满需要5比亚迪元EV360电池容量是42KWH(42度电),但充满需要5比亚迪元EV360电池容量是42KWH(42度电),但充满需要5比亚迪元EV360电池容量是42KWH(42度电),但充满需要5比亚迪元EV360电池容量是42KWH(42度电),但充满需要5"

plaintext=str(plaintext)
# print(plaintext.encode())
# 对字符串进行异或加密
ciphertext = xor_encrypt(plaintext.encode(), key)

# 打印加密后的密文和密钥
print("密文:", ciphertext.hex())
print("密钥:", key.hex())

import binascii

def xor_decrypt(ciphertext, key):
    """对给定的密文进行异或解密"""
    plaintext = bytearray(len(ciphertext))
    for i in range(len(ciphertext)):
        plaintext[i] = ciphertext[i] ^ key[i % len(key)]
    return plaintext.decode()

# 用于解密的密钥和密文
# key = binascii.unhexlify('8f6a71e35bf0c6e7beec02d8c200f5c5')
# ciphertext = binascii.unhexlify('3845b075ae98b1c5f4d4d8c29e')
key=key

ciphertext=ciphertext

# 对密文进行异或解密
plaintext = xor_decrypt(ciphertext, key)

# 打印解密后的明文
print("明文:", plaintext)

import os
import random


def generate_key(length):
    """生成给定长度的随机字节序列"""
    key = bytearray(length)
    for i in range(length):
        key[i] = random.randint(0, 255)
    return key


def xor_encrypt(plaintext, key):
    """对给定字符串进行异或加密"""
    ciphertext = bytearray(len(plaintext))
    for i in range(len(plaintext)):
        ciphertext[i] = plaintext[i] ^ key[i % len(key)]
    return ciphertext


def encrypt_file(filename, key):
    """对给定文件进行加密"""
    # 读取文件内容
    with open(filename, 'rb') as f:
        plaintext = f.read()

    # 对文件内容进行加密
    ciphertext = xor_encrypt(plaintext, key)

    # 将加密后的内容写回文件中
    with open(filename, 'wb') as f:
        f.write(ciphertext)


# 要加密的文件名和密钥长度
filename = 'test.txt'
key_length = 16

# 生成一个长度为16的随机密钥
key = generate_key(key_length)

# 对文件进行加密
encrypt_file(filename, key)

# 打印加密后的密文和密钥
with open(filename, 'rb') as f:
    ciphertext = f.read()
print("密文:", ciphertext.hex())
print("密钥:", key.hex())

my_list = ["apple", "banana", "orange"]
my_text = ", ".join(my_list)
print(my_text)   
# 输出:apple, banana, orange
在这个例子中,我们首先定义了一个包含三个字符串的列表 my_list。然后,我们使用 join() 方法将列表中的所有元素连接起来,并用逗号和空格分隔它们,形成一个新的字符串 my_text。最后,我们打印出这个字符串。您可以使用不同的分隔符(例如换行符、制表符等)来连接列表中的元素,具体取决于您的需求。


from azure.cognitiveservices.speech import AudioDataStream, SpeechConfig, SpeechSynthesizer, SpeechSynthesisOutputFormat
from azure.cognitiveservices.speech.audio import AudioOutputConfig
import azure.cognitiveservices.speech as speechsdk
speech_key, service_region = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "bbbbbbbbbbbbbbbbbbb"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
audio_config = AudioOutputConfig(use_default_speaker=True)
speech_synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
def recognize_from_microphone():
    speech_config.speech_recognition_language="zh-cn" #zh-cn#en-US
    audio_config = speechsdk.audio.AudioConfig(use_default_microphone=True)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
    print("Speak into your microphone.")
    speech_recognition_result = speech_recognizer.recognize_once_async().get()
    if speech_recognition_result.reason == speechsdk.ResultReason.RecognizedSpeech:
        print("Recognized: {}".format(speech_recognition_result.text))
    elif speech_recognition_result.reason == speechsdk.ResultReason.NoMatch:
        print("No speech could be recognized: {}".format(speech_recognition_result.no_match_details))
    elif speech_recognition_result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = speech_recognition_result.cancellation_details
        print("Speech Recognition canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print("Error details: {}".format(cancellation_details.error_details))
            print("Did you set the speech resource key and region values?")

recognize_from_microphone()