title: python - how to get socket connection state?
body: <p><strong>Hi!</strong></p>
  
  <p>I'm beginer in python, and i want to ask here.
  I have code. When I click the button (btn_connect), socket connects to the tv (function self.connection()). Is there way to check socket connection and get connected or disconnected? I want write it to the text field, like "Status: Connected"</p>
  
  <p><strong>Here is my code:</strong></p>
  
  <pre><code>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()
  </code></pre>
  
  <p><strong>Please help me.
  Thank you!</strong></p>