Python and Local Networks

This is something I have never looked into and will do so at this month’s end or sometimes in december…

which is, detecting a PC on a local network and perform read/write operations using python.

I don’t know if that can even be done with python, but if anyone has done something like that before, enlighten me some.

Just thinking about it right off the top of my mind, I can see accessing a local PC setup as a server on a given port (with portforwarding setup on the router).

You would have to know the internal router address for the PC in question.

Example: http://127.0.0.1:60/

If that worked, one could simply write a CGI function for the server and feed it arguments.

Example: http://127.0.0.1:60/proc.cgi?Action=someaction

… I won’t think about it too much right now, I have so much work to do but it’s coming out really nice. I hate Extream programming, but somebody has to do it. :slight_smile:

Well you could create a HTTPServer for every client to receive requests. Here’s an old snippet for stackless, but you could change the request-handling to use threads instead:

import stackless
import socket

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from Server import rpcConnect
import PersistanceServer


body = """
<html>
<head>
<title>StacklessHTTPServer page</title>
</head>
<body>
Nothing to see here, move along..
</body>
</html>
"""



class LoginServer(HTTPServer):
    dbEndpoint = None
    dbService = None
    
    def __init__(self, port):
        HTTPServer.__init__(self, ("", port), LoginRequestHandler)
        self.dbEndpoint, self.dbService = rpcConnect("", PersistanceServer.PORT, "db")
        
    def handle_request(self):
        try:
            request, client_address = self.get_request()
        except socket.error:
            return
        stackless.tasklet(self.handle_request_tasklet)(request, client_address)

    def handle_request_tasklet(self, request, client_address):
        if self.verify_request(request, client_address):
            try:
                self.process_request(request, client_address)
            except:
                self.handle_error(request, client_address)
                self.close_request(request)
        


class LoginRequestHandler(BaseHTTPRequestHandler):
    # Respect keep alive requests.
    protocol_version = "HTTP/1.1"

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.send_header("Content-Length", len(body))
        self.end_headers()

        self.wfile.write(body)

As for discovering clients on a LAN:
Make your clients broadcast some kind of announcement over UDP and let them also listen for it. If you (as a client) find a new mate you can append it to a ‘known’ list.

It’s just a rough idea, never tried it myself. Maybe the others have more suitable solutions.

Andreas

It seems my first thought (openning post), was accurate enough; my solution worked with little stress.

I just have a question now about urlopen, from urllib.request…

Once I trigger serverB’s cgi handler,

KK = urlopen(GG)

Do I have to cleanup the object KK with some cleanup method/function? I was looking through python 3.3 docs and could not find a clear answer (help doc writters now and days have no mercy).

I will be calling to serverB’s cgi a lot and do not wish to flood out memory because I’m creating a ton of objects and not destroying any.

I’m guessing I won’t have an issue with that now. What I did was, instead of using a constant running program, I have a main cgi which executes the action and returns. When a cgi process finishes, it frees from memory anyway.

So… That’s that… I guess. :laughing:

Man… The things I do for elderly people. She wanted the best one on the net, so that what she’s going to get. :smiley: