python--信息自动发送器

源码实现

import win32gui
import win32api
import win32con
import win32clipboard as w
import time
import optparse
import random
import requests


def ctrlV():
    win32api.keybd_event(17,0,0,0)  #ctrl键位码是17
    win32api.keybd_event(86,0,0,0)  #v键位码是86
    win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0) #释放按键
    win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)

def send(name, msg,sleep_time):
    #打开剪贴板
    w.OpenClipboard()
    #清空剪贴板
    w.EmptyClipboard()
    #设置剪贴板内容
    w.SetClipboardData(win32con.CF_UNICODETEXT, msg)
    #获取剪贴板内容
    date = w.GetClipboardData()
    #关闭剪贴板
    w.CloseClipboard()
    #获取窗口句柄
    handle = win32gui.FindWindow(None,name)
    if handle == 0:
        print('---------为打开该窗口------')
    #显示窗口
    win32gui.ShowWindow(handle,win32con.SW_SHOW)
    #把剪切板内容粘贴到qq窗口
    ctrlV()
    time.sleep(sleep_time)#延缓进程
    #win32gui.SendMessage(handle, win32con.WM_PASTE, 0, 0)
    #按下后松开回车键,发送消息
    win32gui.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(handle, win32con.WM_KEYUP, win32con.VK_RETURN, 0)

def banner():
    print("""
           _______ .__   __.  _______   ____   ____    ____  ___   .______      
          |   ____||  \ |  | |       \ |___ \  \   \  /   / / _ \  |   _  \     
          |  |__   |   \|  | |  .--.  |  __) |  \   \/   / | | | | |  |_)  |    
          |   __|  |  . `  | |  |  |  | |__ <    \      /  | | | | |      /     
          |  |____ |  |\   | |  '--'  | ___) |    \    /   | |_| | |  |\  \----.
          |_______||__| \__| |_______/ |____/      \__/     \___/  | _| `._____|
          """)

def RUN():
    banner()
    print('------RUN-----')
    usage = 'python %prog -u/--user <target user> / -m/--msg <target message> -t/--time <target times>'
    parser = optparse.OptionParser(usage)
    parser.add_option('-u','--user',dest="user",type="string",help = "target user",default="cyh这辈子最爱的唯一亲老婆")
    parser.add_option('-m','--msg',dest="message",type="string",help = "target message",default = "我永远爱你!")
    parser.add_option('-t','--time',dest="time",type="int",help = "target times",default = 5)
    parser.add_option('-s','--sleep',dest="sleep",type="float",help = "target sleep time",default = 1)
    parser.add_option('-c','--choice',dest="choice",type="string",help = "function selection",default = '1')
    opts,args = parser.parse_args()
    if(opts.choice == '1'):
        for i in range(0, opts.time):
            print("发送成功:"+opts.message)
            send(opts.user, opts.message,opts.sleep)
        print('-----over----')
    else:
        for i in range(0, opts.time):
            message = sentence(opts.choice)
            print("发送成功:"+message)
            send(opts.user,message,opts.sleep)
        print('-----over----')
def sentence(choice):
    url = "https://v1.hitokoto.cn/?c="+ choice +"&encode=text&min_length=15"
    r = requests.post(url)
    return r.text

if __name__ == "__main__":
    RUN()