你好!
我是python的初学者,我想在这里问一下。 我有代码。当我单击按钮(btn_connect)时,套接字连接到tv(函数self.connection())。有没有办法检查套接字连接并连接或断开连接?我想将它写入文本字段,例如"状态:已连接"
这是我的代码:
import fcntl, socket, struct
import base64
import time, datetime
import netifaces
from Tkinter import *
def getMyMac1(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]])
myip = netifaces.ifaddresses(netifaces.gateways()['default'][netifaces.AF_INET][1])[netifaces.AF_INET][0]['addr']
mymac = getMyMac1(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);
root = Tk()
root.title("Pepin's Samsung TV Remote")
root.geometry("391x595")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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
messagepart2 = chr(0xc8) + chr(0x00)
part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart2)) + chr(0x00) + messagepart2
class Application():
"""Pepin's Samsung TV Remote"""
def __init__(self, master):
self.master = master
self.create_widgets()
def connection(self):
sock.connect((self.entry_tvip.get(), 55000))
sock.send(part1)
sock.send(part2)
self.tvappstring = app.entry_tvappstring.get()
def create_widgets(self):
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()
请帮帮我。 谢谢!
答案 0 :(得分:1)
连接套接字:
您的流连接套接字块和recv()在另一端关闭时返回“”。所以有你的状态。
<强> UDP:强>
数据报UDP套接字是广播的,因此状态有点模糊,当然与连接的相比。
你的电视小工具肯定有关于的文件 UDP的流量对吗?例如sendto()状态请求或一些等效概念。
http://www.binarytides.com/programming-udp-sockets-in-python/