Python - connection.close() - SyntaxError: invalid syntax
Question: I am trying to create quote of the day server, and I am having a problem with
sockets. My program code is below:

    
    
    #!/usr/bin/python
    
    from socket import *
    from  ConfigParser import *
    import sys
    import argparse
    import random
    
    class serverConf:
        port = 17
        host = ""
        quotefile = ""
    
    def initConfig(filename):
    
    
        config = ConfigParser()
    
        config.add_section('Server')
        config.set('Server', 'host', '')
        config.set('Server', 'port', '17')
    
        config.add_section('Quotes')
        config.set('Quotes', 'file', 'quotes.txt')
    
    
    
    
        with open(filename, 'w') as configfile:
            config.write(configfile)
    
    
    def parseConfig(filename):
    
        configOptions = serverConf()
    
    
        try:
            config = ConfigParser()
            config.read(filename)
    
    
    
            configOptions.port = config.getint('Server', 'port')
            configOptions.host = config.get('Server', 'host')
            configOptions.quoteFile = config.get('Quotes', 'file')
    
        except KeyboardInterrupt:
            raise
    
        except:
            print "[Info] Configuration file \'" + filename + "\' does not exist. Creating one with the default values"
    
            configOptions.port = 17
            configOptions.host = ""
            configOptions.quoteFile = "quotes.txt"
    
            try:
                initConfig(filename)
    
            except KeyboardInterrupt:
                raise
    
            except:
    
                print "[Fatal] Unable to create configuration file \'" + filename + "\'. This program must exit"
                sys.exit()
    
    
    
    
    
        print "[Info] Read configuration options"
    
        return configOptions
    
    def doInitMessage():
    
        print "Quote Of The Day Server"
        print "-----------------------"
        print "Version 1.0 By Ian Duncan"
        print ""
    
    def random_line(afile):
        line = next(afile)
        for num, aline in enumerate(afile):
          if random.randrange(num + 2): continue
          line = aline
        return line
    
    def doCheckQuotesFile(quotesFilename):
    
        try:
    
            with open(quotesFilename): pass
    
            print "[Info] Discovered file containing quotes \'" + quotesFilename + "\'"
    
        except IOError:
    
            print "[Fatal] Unable to read quotes file \'" + quotesFilename + "\'. This program must exit"
            sys.exit()
    
    def Start(args):
    
        filename = "qotdconf.ini"
    
        doInitMessage()
    
        configOptions = parseConfig(filename)
    
    
    
        if configOptions.host == '':
            print "[Info] Will start server at: " + "*"  + ":" + str(configOptions.port)
        else:
            print "[Info] Will start server at: " + configOptions.host + ":" + str(configOptions.port)
    
        doCheckQuotesFile(configOptions.quoteFile)
    
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.bind ( (configOptions.host, configOptions.port) )
            s.listen (5)
    
            print "[Info] The server is now broadcasting"
    
            while 1:
                    connection, address = s.accept()
                    print("[Info] Accepted Connection from " +  str(address) + "!")
    
                    connection.send(random_line(configOptions.quoteFile + "\n")
    
                    connection.close()
    
        except socket.error:
    
            print "[Fatal] There was a problem. This program must exit"
            sys.exit()
    
    
    
    parser = argparse.ArgumentParser(description='A quote of the day server.')
    parser.add_argument('--port', dest='accumulate', action='store_const', const=int, default=max, help='the port the server broadcasts on')
    parser.add_argument('--host', dest='accumulate', action='store_const', const=str, default=max, help='the ip address or domain name the server broadcasts on')
    parser.add_argument('--quotes', dest='accumulate', action='store_const', const=str, default=max, help='the file that stores the quotes')
    parser.add_argument('--config', dest='accumulate', action='store_const', const=str, default=max, help='specify a custom configuration file')
    
    args = parser.parse_args()
    
    Start(args)
    

When I run this, I get:

    
    
      File "server.py", line 129
        connection.close()
                 ^
    SyntaxError: invalid syntax
    

In other instances where I had problems, Python pointed me to the problem
immediately. This time, I have no idea what is going on. What is causing this
error?


Answer: Ah, it's the ol' missing parenthesis on the preceding line again...

    
    
    connection.send(random_line(configOptions.quoteFile + "\n")
    

Because Python automatically continues statements across lines inside
parentheses, it's seeing this statement and the next as one line:

    
    
    connection.send(random_line(configOptions.quoteFile + "\n") connection.close()
    

And clearly that doesn't make any sense (it needs _something_ before
`connection.close()`, and _also_ still needs a closing parenthesis).

So you want:

    
    
    connection.send(random_line(configOptions.quoteFile + "\n"))
    

