Need help with TCP/IP client access from Windows

Eric S. Raymond esr at snark.thyrsus.com
Thu Jul 29 16:32:34 EDT 1999


I need a Windows equivalent of this class for a client I'm writing.

class SessionConnection:
    # Encapsulate the line-by-line level of a session with a server.
    # This class requires Unix services.
    param_names = ('host', 'port', 'username', 'password', 'browse')

    def __init__(self, file=".intriguer"):
        # Initialize a SessionConnection object from user's stored paramters
        self.dotfile = os.path.join(os.environ["HOME"], file)
        self.host = "snark.thyrsus.com"
        self.port = 99999
        self.username = "esr at thyrsus.com"
        self.password = "fooup"
        self.browse = "netscape -remote 'openURL(%s)'"
        try:
            dfp = open(self.dotfile);
            lexer = shlex.shlex(dfp)
            while 1:
                variable = lexer.get_token()
                if not variable:
                    break
                if not variable in  Session.param_names:
                    raise SyntaxError
                if lexer.get_token() != '=':
                    raise SyntaxError
                value = lexer.get_token()
                if not value:
                    raise SyntaxError
                setattr(self, variable, value);
            dfp.close()
        except IOError:
            pass	# if file is absent, fall back to defaults.
        self.socket = None
        self.sockfp = None

    def __repr__(self):
        # Dump current session parameters
        res = "# intriguer configuration\n"
        for p in Session.param_names:
            if getattr(self, p):
                res = res + "%s=%s\n" % (p, `getattr(self, p)`)
        return res

    def wrapup(self):
        # Update the dotfile to reflect parameter edits
        dfp = open(self.dotfile, "w")
        dfp.write(`self`);
        dfp.close()

    # Establish and break server connections

    def connect(self):
        # Initialize a server connection.
        # Not part of the class initialization sequence only because
        # we might want to create an instance just to examine or
        # edit the parameters.
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect(self.host, self.port)
        self.sockfp = self.socket.makefile("r+")
        
    def disconnect(self):
        # Terminate a server connection
        if self.socket:
            self.socket.close()
            self.sockfp.close()

    # Communication with server

    def readline(self):
        # Read a raw line from the server
        return self.sockfp.readline()

    def writeline(self, line):
        # Write a raw line to the server
        return self.sockfp.write(line + "\n")

    # Local service invocations

    def browse_url(self, url):
        # Point a local browser at a specified URL 
        os.system(self.browse % url)

The issues here are:

(1) How do I get (at least) line-oriented access to an Internet server
    from a Python program running under Windows?

(2) How does one read and write the Windows equivalent of a dotfile?
    Must this involve the (shudder) registry?

(3) Is there a reliable way for Python programs under Windows to point a local
    browser at a specified URL?
-- 
		<a href="http://www.tuxedo.org/~esr">Eric S. Raymond</a>




More information about the Python-list mailing list