title: trying to send data from one script in python to another script
body: <p>I am trying to write a script so when an IP address can't be seen a message gets sent with a telegram letting me know which computer is offline</p>
  <p>I have been able to get the telegram side working but i have not been able to pass the data from the main script where it is testing the ip address , at the moment i have test data in there but i would like it to send the error with the computer name</p>
  <p>main.py</p>
  <pre class="lang-py prettyprint-override"><code>import socket
  import ssl
  from datetime import datetime
  import pickle
  
  import subprocess
  import platform
  
  class Server:
      def __init__(self, name, port, connection, priority):
          self.name = name
          self.port = port
          self.connection = connection.lower()
          self.priority = priority.lower()
  
          self.history = []
          self.alert = False
  
      def check_connection(self):
          msg = &quot;&quot;
          success = False
  
          now = datetime.now().strftime(&quot;%d-%m-%Y %H:%M&quot;)
  
          try:
              if self.connection == &quot;plain&quot;:
                  socket.create_connection((self.name, self.port), timeout=10)
                  msg = f&quot;{self.name} is up. On port {self.port} with {self.connection}&quot;
                  success = True
                  self.alert = False
              elif self.connection == &quot;ssl&quot;:
                  ssl.wrap_socket(socket.create_connection((self.name, self.port), timeout=10))
                  msg = f&quot;{self.name} is up. On port {self.port} with {self.connection}&quot;
                  success = True
                  self.alert = False
              else:
                  if self.ping():
                      msg = f&quot;{self.name} is up. On port {self.port} with {self.connection}&quot;
                      success = True
                      self.alert = False
          except socket.timeout:
              msg = f&quot;server: {self.name} timeout. On port {self.port}&quot;
          except (ConnectionRefusedError, ConnectionResetError) as e:
              msg = f&quot;server: {self.name} {e}&quot;
          except Exception as e:
              msg = f&quot;No Clue??: {e}&quot;
  
          if success == False and self.alert == False:
              # Send Alert
              self.alert = True
              import tg_start
              tg_start.send_message(&quot;Happy days&quot;)
  
          self.create_history(msg, success, now)
  
      def create_history(self, msg, success, now):
          history_max = 100
          self.history.append((msg, success, now))
  
          while len(self.history) &gt; history_max:
              self.history.pop(0)
  
      def ping(self):
          try:
              output = subprocess.check_output(&quot;ping -{} 1 {}&quot;.format('n' if platform.system().lower(
              ) == &quot;windows&quot; else 'c', self.name), shell=True, universal_newlines=True)
              if 'unreachable' in output:
                  return False
              else:
                  return True
          except Exception:
              return False
  
  
  if __name__ == &quot;__main__&quot;:
      try:
          servers = pickle.load(open(&quot;servers.pickle&quot;, &quot;rb&quot;))
      except:
          servers = [
              # Server(&quot;ifmc-repserver&quot;, 80, &quot;plain&quot;, &quot;high&quot;),
              # Server(&quot;ifmc-repserver&quot;, 80, &quot;plain&quot;, &quot;high&quot;),
              # Server(&quot;ifmc-repserver&quot;, 465, &quot;ssl&quot;, &quot;high&quot;),
              # Server(&quot;ifmc-repserver&quot;, 80, &quot;ping&quot;, &quot;high&quot;),
              Server(&quot;ifmc-repserver&quot;, 80, &quot;ping&quot;, &quot;high&quot;)
          ]
  
      for server in servers:
          server.check_connection()
          print(len(server.history))
          print(server.history[-1])
  
      pickle.dump(servers, open(&quot;servers.pickle&quot;, &quot;wb&quot;))
  </code></pre>
  <p>and tg_start.py</p>
  <pre class="lang-py prettyprint-override"><code>import requests
  
  message = &quot;global&quot;
  alert = &quot;&quot;
  
  def send_message(text):
      global alert
      global message
      print (&quot;this is text messsage&quot; + &quot; &quot; + text)
      #text = &quot;Superman&quot;
      alert = text
      print(&quot;Sending ALERT ...&quot;)
      token = &quot;token&quot;
      chat_id = &quot;chat_id&quot;
      print(&quot;test message&quot; + &quot; &quot; + alert)
  
      url_req = &quot;https://api.telegram.org/bot&quot; + token + &quot;/sendMessage&quot; + &quot;?chat_id=&quot; + chat_id + &quot;&amp;text=&quot; + alert
  
      print(url_req)
      #results = requests.get(url_req)
      results = requests.post(url_req) # this request is a post, not a get
  
      print(results.json())
      # text = &quot;my name&quot; + text
  
  
  send_message(alert)
  
  </code></pre>