Python - 从代码底部提取内容

时间:2015-06-08 16:27:31

标签: python

我有这段代码:

import fcntl, socket, struct
import base64
import time, datetime
import netifaces
from Tkinter import *

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return '-'.join(['%02x' % ord(char) for char in info[18:24]])

tvip = "10.0.1.2"
tvappstring = "UE55C8000"
myip = netifaces.ifaddresses(netifaces.gateways()['default'][netifaces.AF_INET][1])[netifaces.AF_INET][0]['addr']
mymac = getHwAddr(netifaces.gateways()['default'][netifaces.AF_INET][1])
appstring = "Pepin's Samsung TV Remote"
remotename = "Pepin's Samsung TV Remote"

def sendKey(skey, dataSock, appstring):
 messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len(
base64.b64encode(skey))) + chr(0x00) + base64.b64encode(skey);
 part3 = chr(0x00) + chr(len(appstring)) + chr(0x00) \
+ appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3
 dataSock.send(part3);

def sendText(stext, dataSock, appstring):
 messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len(
base64.b64encode(stext))) + chr(0x00) + base64.b64encode(stext);
 part3 = chr(0x00) + chr(len(appstring)) + chr(0x00) \
+ appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3
 dataSock.send(part3); 

root = Tk()
root.title("Pepin's Samsung TV Remote")
root.geometry("391x595")

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((tvip, 55000)) 
ipencoded = base64.b64encode(myip)
macencoded = base64.b64encode(mymac)
messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \
+ chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \
+ macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \
+ base64.b64encode(remotename)

part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart1)) + chr(0x00) + messagepart1
sock.send(part1)

messagepart2 = chr(0xc8) + chr(0x00)
part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart2)) + chr(0x00) + messagepart2
sock.send(part2);

class Application():
    """Pepin's Samsung TV Remote"""

    def __init__(self, master):
        self.master = master
        self.create_widgets()

    def create_widgets(self):

        btn_key_poweroff = Button(self.master, text = "Power", bg="red", width=4, height=2, command = lambda: sendKey("KEY_POWEROFF", sock, tvappstring)) 

        entry_send_custom = Entry(self.master, width=22)
        entry_send_custom.grid(row=1, column=3, columnspan=4, padx=(15,0), ipady=8)

        btn_send_custom = Button(self.master, text = "SEND CUSTOM KEY", width=19, height=2, command = lambda: sendText(entry_send_custom.get(), sock, tvappstring)) 
            btn_send_custom.grid(row=2, column=3, columnspan=5, padx=(15,0))

        label_tvip = Label(self.master, text="TV IP:")
        label_tvip.grid(row=3, column=3, columnspan=1, padx=(15,0), ipady=8)

        self.entry_tvip = Entry(self.master, width=16)
        self.entry_tvip.grid(row=3, column=4, columnspan=3, ipady=8)

        label_tvappstring = Label(self.master, text="TV MODEL (Tv App String)")
        label_tvappstring.grid(row=4, column=3, columnspan=4, padx=(15,0), ipady=8, sticky=W)

        self.entry_tvappstring = Entry(self.master, width=22)
        self.entry_tvappstring.grid(row=5, column=3, columnspan=4, padx=(15,0), ipady=8)
        self.entry_tvappstring.insert(0, "UE55C8000")

        btn_connect = Button(self.master, text = "CONNECT TO TV", width=19, height=2, command = lambda: self.Connection()) 
            btn_connect.grid(row=6, column=3, columnspan=5, padx=(15,0))     

app = Application(root)
root.mainloop()


sock.close()

但我想在代码顶部使用entry_tvip.get()

有简单的方法吗?

然后开始代码的这一部分:

sock.connect((tvip, 55000)) 
ipencoded = base64.b64encode(myip)
macencoded = base64.b64encode(mymac)
messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \
+ chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \
+ macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \
+ base64.b64encode(remotename)

part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart1)) + chr(0x00) + messagepart1
sock.send(part1)

messagepart2 = chr(0xc8) + chr(0x00)
part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart2)) + chr(0x00) + messagepart2
sock.send(part2);

按下按钮后。还有办法吗?

谢谢!我是Python的新手

1 个答案:

答案 0 :(得分:1)

由于entry_tvipApplication类的方法而app是此类的实例,因此您可以调用app.entry_tvip.get()

在创建app之前,您无法在任何地方执行此操作,因此我会重新考虑您的方法。