Search code examples
pythonconfiguration-files

SafeConfigParser overriding files


I am making a chat application, and need a conf file for the server and another for the client. However, whenever I run the client its conf properties get transferred to the configuration file of the server even though I am using SafeConfigParser. Is there any way to fix this? Thanks.

when server starts:

config = configparser.SafeConfigParser()
config.read('chatserver.conf')
config['PORT']['port'] = str(self.port)
with open("chatserver.conf","w") as configfile:
   config.write(configfile)

when a client joins:

clientconf = configparser.SafeConfigParser()
clientconf.read('chatclient.conf')
clientconf['SERVER']['last_server_used'] = str(self.host)
clientconf['SERVER']['port_used'] = str(self.port)
with open("chatserver.conf","w") as confFile:
    clientconf.write(confFile)

chatclient.conf:

[SERVER]
last_server_used = '127.0.0.1'
port_used = '50000'
default_debug_mode = False
log = True
default_log_file = chat.log

chatserver.conf:

[PORT]
port = '1000'

When I run the server, a client joins the chat, and close it, then run the server again, the chatserver.conf file becomes the same as chatclient.conf


Solution

  • In the line:

    with open("chatserver.conf","w") as confFile:
        clientconf.write(confFile)
    

    You save clientconf as chatserver.conf. I think you mean to save as chatclient.conf instead.