0

I want to make a connection to a database. Depending on the computer the script is running it should be a remote connection (windows computer) or a direct connection (raspberry pi).

I'm using the script below but I think there should be a much nicer way to do this (no try except). I'm using the try except because os.environ on windows and raspberry pi doesn't have an identical ID.

import os
try:
    if os.environ['USERDOMAIN'] == 'DESKTOP-name':
        print('Make windows conncetion')
except:
    if os.environ['HOSTNAME'] == 'raspberrypi':
        print('Make raspberry connection')
James
  • 32,991
  • 4
  • 47
  • 70
Oamriotn
  • 257
  • 1
  • 3
  • 8
  • 1
    To detect your os look at https://stackoverflow.com/questions/1854/what-os-am-i-running-on . Additionally regarding using dictionaries without needing the try statement see https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary – tomgalpin Feb 27 '20 at 10:14
  • 1
    `try` / `except` is fine. Don't worry about it. – martineau Feb 27 '20 at 10:16

0 Answers0