Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Sockets & IP's (http://www.programmingforums.org/showthread.php?t=15460)

Freaky Chris Mar 23rd, 2008 2:34 AM

Sockets & IP's
 
Hi, im new to python just so you know
this maybe hard to explain so if you don't inderstand what im asking just say.

Im am trying to write a scripts that connects to a remote pc, this is not a problem providing i have the IP of the computer. However thecomputer i want to connect to is on a network, so the IP address im having to try to connect to is the routers IP and not the indervidual computers IP. Is there anyway using socket.connect() or another function perhaps to connect to a remote pc on a network.

thanks for any help

Ooble Mar 23rd, 2008 3:22 AM

Re: Sockets & IP's
 
If you pass the function you're using the network IP address of the computer, shouldn't the router point you directly to the computer you're looking for?

Freaky Chris Mar 23rd, 2008 3:30 AM

Re: Sockets & IP's
 
yes it does for local, but this is remote. As far as im awear you can only pass it one ip address, which firstly must be the address of the remote router from there it needs to specify which computer to connect to on the network.

Ooble Mar 23rd, 2008 3:59 AM

Re: Sockets & IP's
 
My recommendation would be to have the router always point to the same PC for the port you're connecting on, and have that PC redirect the query based on the network address you send it.

Freaky Chris Mar 23rd, 2008 4:28 AM

Re: Sockets & IP's
 
im looked into that and after configuring the virtual server on my router to forward calls to 4000 to a specified computer on my network and the port that the server script is monitoring however still no luck, im getting a connection timed out error. This may be due to the fact im testing internally although this should affect it.

Any other ideas?

Ooble Mar 23rd, 2008 7:56 AM

Re: Sockets & IP's
 
Sounds like a problem with your server script. Can we see it?

Freaky Chris Mar 23rd, 2008 11:00 AM

Re: Sockets & IP's
 
Sure, just remember it works fine on a local network so im not sure that it is the server script but here. There are a few things that arn't completely tied up as im just testing things out but it basically works.

:

import socket, sys, threading

def parseIP(ip):
    unparsedIP = ip
    parsedIP = ''
    parsedIP = unparsedIP.replace('(\'', '')
    parsedIP = parsedIP.replace('\',', '')
    parsedIP = parsedIP.replace(')', '')
    x = parsedIP.find(' ')
    parsedIP = parsedIP[:x]
    return parsedIP

def msgclient(client):
    address = myClients[client]
    return address

class server(threading.Thread):
    v = ''
    vlock = threading.Lock()
    id = 0
   
    def __init__(self, clientSock):
        threading.Thread.__init__(self)
        self.myid = server.id
        server.id += 1
        self.myclientsock = clientSock

    def run(self):
        while 1:
            k = self.myclientsock.recv(1000000)
            if k == '/close': break
            elif k == '/show':
                msg = '\n'.join(myClientsNames)
                self.myclientsock.send(msg)
            elif k == '/terminate':
                k = ''
                server.v = ''
                break
            elif k[:5] == '/send':
                t = k.lstrip('/send ')
                q = t.find(' ')
                myClientsSock[((t[:q+1]).rstrip(' ')).title()].send('HELLO')
                print 'HELLO'
                client.send('sent!')
            else:
                server.vlock.acquire()
                server.v += k
                server.vlock.release()
                client.send(server.v)
        myClientsNames.remove(myClients[self.myclientsock])
        del myClientsSock[myClientsNames[myClients[self.myclientsock]]]
        del myClientsIPs[myClientsNames[myClients[self.myclientsock]]]
        self.myclientsock.close()
       
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(('',4000))
sock.listen(5)

noClients = 5
mythreads = []
myClients = {}
myClientsIPs = {}
myClientsNames = []
myClientsSock = {}

for i in range(noClients):
    (client, ap) = sock.accept()
    uName = (client.recv(20)).title()
    myClients[client] = uName
    myClientsIPs[uName] = parseIP(str(ap))
    myClientsNames.append(uName)
    myClientsSock[uName] = client
    newServer = server(client)
    print ap
    mythreads.append(newServer)
    newServer.start()

sock.close()


Ooble Mar 24th, 2008 3:08 AM

Re: Sockets & IP's
 
Was thinking about this earlier and found out my router doesn't allow external-style access when you're accessing things internally. If you're inside the network, you have to do things one way; if you're outside, you have to do it a different way. Perhaps that's causing the problem? Have you tried accessing it from outside?

Freaky Chris Mar 24th, 2008 4:44 AM

Re: Sockets & IP's
 
i did have a feeling that may of been a problem, no i havn't had chance yet but i will a soon as i can.
Did you have a look at my code at all? if so did you see any major errors if not then its ok just wondered

thanks,

Ooble Mar 24th, 2008 5:27 AM

Re: Sockets & IP's
 
I'm not seeing any problems, though I've only really glanced over it. As you say though, it works fine locally, so there's no real reason it shouldn't work fine Internet-wide.


All times are GMT -5. The time now is 9:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC