1

I want to modify a hostapd config file using python

#
# Wireless Interface
#
interface=wlp2s0
driver=nl80211
#
# Wireless Environment
#
# Currently not working due to
# regulatory restrictions on 5GHz wifi
# in driver
#
ssid=bobthebuilder
hw_mode=a
ieee80211d=1
country_code=GB
channel=40
ieee80211n=1

(etc)

But the configparser library requires headers for config sections to use - us there another library that I can use to edit this file instead?

Rob
  • 335
  • 8
  • 23

1 Answers1

7

Seems like a very simple config file syntax. Why not just write your own config parser?

conf = {}
with open('config.cfg') as fp:
  for line in fp:
    if line.startswith('#'):
      continue
    key, val = line.strip().split('=')
    conf[key] = val
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • `.split('=', 1)` is slightly better in case the value has an `=` in it. – Nathan Villaescusa May 09 '18 at 09:26
  • Yeah. This is just meant as a very minimal example. For production use, you would probably want some syntax validation as well. But I would also highly recommend using some standard config format instead. Typical choices for config files are ini format (what configparser expects), yaml or json. – Håken Lid May 09 '18 at 09:31