#-*- coding: big5 -*- #-*- coding: cp950 -*- def postbbs(): import telnetlib import time tn = telnetlib.Telnet('ptt.cc') tn.set_debuglevel(2) def login (id,pw) : print '>>>login<<<<' tn.expect(["註冊: "]) tn.write(id+"\r" ) tn.expect(["密碼:"]) tn.write(pw + "\r") def prework(): print '>>>Prework<<<<' q=tn.expect([ "您想刪除其他重複登入的連線嗎?", '任意鍵繼續', '您有一篇文章尚未完成', '0-9', '您要刪除以上錯誤嘗試的記錄嗎?', '請砍過長的水球記錄或信件', '鴻雁往返', '呼叫器']) print '>>>Prework - ',q[0],'<<<<' if q[0]==0 : tn.write('Y' + "\r") #tn.write("\r") return prework() elif q[0]==1 : tn.write("p") return prework() elif q[0]==2 : time.sleep(1) tn.write("Q\r") return prework() elif q[0]==3 : time.sleep(1) tn.write("0\r") return prework() elif q[0]==4 : tn.write("\r") return prework() elif q[0]==5 : tn.write("\r") return prework() elif q[0]==6 : tn.write("\x1b\x4f\x44") return prework() elif q[0]==7 : return True def toBoard(board) : print '>>>toBoard<<<<' tn.write('f' + "\r") q=tn.expect(['選擇看板']) tn.write('y' ) #q=tn.expect(['篩選列表']) print '>>sleep 2s<<' time.sleep(2) tn.write('s' ) q=tn.expect(['請輸入看板名稱']) tn.write(board + "\r") print '>>sleep 2s<<' time.sleep(2) tn.write("\r") print '..............>>expect continue<<...........' q=tn.expect(['請按任意鍵繼續','進板畫面']) if q[0]==0 : time.sleep(1) tn.write("p") print '..............>>expect 進板畫面<<............' q=tn.expect(['進板畫面']) else : print '..............>>no Logo..<<...........' def post (title,content): #http://pcman.ptt.cc/control.html print '..............>>ctrl + p<<..................' tn.write('\x10') #ctrl+p #這裡送出的\x10 總是無法正確的發表文章 都會變成進入觀看文章 #以下未成功測試過 print '>>expect 不選<<' q=tn.expect(['不選']) time.sleep(0.1) tn.write("\r") print '>>expect 標題<<' q=tn.expect(['標題']) time.sleep(0.1) tn.write(title+"\r") print '>>expect 編輯文章<<' q=tn.expect(['編輯文章']) time.sleep(0.1) tn.write(content) #ctrl+x tn.write('\x18') time.sleep(2) tn.write("\r") print '>>expect 隨機<<' q=tn.expect(['隨機']) time.sleep(2) tn.write("\r") print '>>expect 請按任意鍵繼續<<' q=tn.expect(['請按任意鍵繼續']) time.sleep(2) tn.write("p") login('id','pw') #***帳號,密碼***登入 prework() #處理登入後到主選單前的一些對應 #toBoard('C_Chat') #跳到看板 toBoard('test') t="[電台] Diva Project " c=""" 系統自動播放 點播預計至01 :00分關閉 \r \r 網址\r http://diva.twbbs.org/diva or http://diva-project.appspot.com/diva \r 進入後點 「搜尋/點播」 輸入關鍵字找歌曲 就可以點播\r 「artist參考列表」有一些歌手可以參考\r 收聽\r 按下Diva網站收聽網址旁的「點此播放」\r 或將收聽網址( http://sradio.twbbs.org:8000/diva )\r 貼到慣用的播放器 (例如:檔案->開啟URL)\r \r ***此文由系統自動發出,若系統錯亂連發,請立即水桶這個帳號@@***\r """ post(t,c) #發表文章 tn.close() import traceback , sys try : postbbs() except : f=open('bbs_err.log','a') import time f.write(time.asctime()+"\r\n") f.write(traceback.format_exc()) f.write("\r\n\r\n")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question