TCP sockets in Python question

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

TCP sockets in Python question

Post by eljonto on Sat Oct 18, 2008 4:56 pm
([msg=13883]see TCP sockets in Python question[/msg])

Hey all,
im writing a python program that uses sockets. Im using a tcp connection (SOCK_STREAM) and until now all testing has been done on my computer with the server and client- not on remote computers. Since im new to sockets- i just have this question- i know that the client has to connect to the server with the servers ip address- but does the server need the clients ip address to accept connections from it? because in my client server scripts that work- the client uses a connect() and the server uses a bind(). i read (from google) that binding is just binding to an address which is neccessary for it to listen() for connections from the client. Also- when 'binding' does the server bind itself to whatever- or to the client??
to reiterate- before i test on remote comps- does the server need the clients ip adress?
thanks alot
~ELJONTO
-Quis custodiet ipsos custodes?, Juvenal
_________________________________________________________________
User avatar
eljonto
Poster
Poster
 
Posts: 373
Joined: Thu Apr 17, 2008 1:16 am
Location: Australia
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by nathandelane on Fri Oct 24, 2008 9:18 am
([msg=14027]see Re: TCP sockets in Python question[/msg])

No, your server doesn't need to know the client's ip address, but that is part of the information that is sent in a TCP/IP packet anyway, so you could get it from the first time the client tries to connect. The only reason that you might want to get the client's ip address is in order to suppress connections from a particular client (i.e. create a black list) or vise versa, and only allow certain clients to connect to your server (i.e. a white list).
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by eljonto on Thu Oct 30, 2008 12:22 am
([msg=14270]see Re: TCP sockets in Python question[/msg])

ahh ok thanks-
for various reasons it wasn't working over remote computers- ive tried binding server to '' and then port, 'localhost', port, ive also tried 'INNADDR_ANY',port - but this along with 'gethostname()',port causes an info error :|
on my friends comp i think that windows might've been blocking the server accepting connection though- want me to post example code to have a look?
-Quis custodiet ipsos custodes?, Juvenal
_________________________________________________________________
User avatar
eljonto
Poster
Poster
 
Posts: 373
Joined: Thu Apr 17, 2008 1:16 am
Location: Australia
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by tgoe on Sun Nov 02, 2008 4:54 am
([msg=14380]see Re: TCP sockets in Python question[/msg])

Post it
User avatar
tgoe
Contributor
Contributor
 
Posts: 633
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by eljonto on Mon Nov 03, 2008 6:22 pm
([msg=14442]see Re: TCP sockets in Python question[/msg])

ok, heres the server:
Code: Select all
import socket
import urllib #needed for other features which i didn't include here for brievity

s = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
s.bind ( ( '', 8642 ) )

s.listen ( 1 )
while True:
   channel, details = s.accept()
   while(1):
       data= channel.recv ( 100 )
       command=data.split(" ")
       if not data:
           break
       else:
           if command[0] == "/terminate":
               channel.send("Client exit")
               channel.close()
               s.close()
               exit(1);
           elif command[0] == "/quit":
               channel.send("Client exit")
           else:
               print data
               str="received: "+data
               channel.send(str)
   channel.close()


and here's the client:
Code: Select all
import socket
import urllib

print "<<CLIENT>>\n\n"
print ">>connecting to server...\n"

print ">>Attempting to connect to Server..."
s = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
CONNECT = 0
while CONNECT == 0:
    try:
        s.connect ( ( '127.0.0.1', 8642 ) ) 
    except socket.error, msg:
        continue
    CONNECT = 1
   
print "\n>>Connected to server\n"
 
while (1):
    data=raw_input(">> ")
    if not data:
        break
    else:
        if(s.send(data)):
            retdata=s.recv(1000)                 
            if (retdata != "Client exit" ):
                print ">>Server: ",retdata,"\n"
            else:
                print ">>Closing connection"
                s.close()
                print ">>Connection terminated"
                exit(1);
   
s.close()


obviously replace 127.0.0.1 with the servers remote ip address if used remotely, but i think the problem is either with the server binding, or with firewalls etc
-Quis custodiet ipsos custodes?, Juvenal
_________________________________________________________________
User avatar
eljonto
Poster
Poster
 
Posts: 373
Joined: Thu Apr 17, 2008 1:16 am
Location: Australia
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by Steve8x on Mon Nov 03, 2008 9:39 pm
([msg=14447]see Re: TCP sockets in Python question[/msg])

I am not familiar with python, However I do socket programming in C++ all the time (for windows aka Winsock) At a glance your code seems fine...

Were you running the server from your friends PC and then running the client on your PC and trying to connect to the server? or vice versa?

Which ever computer you make run the server, has to be PORT FORWARDED! What I mean by that is ANY firewalls have to be open on the port your server is "listening" on... thats how servers work, they listen for incoming clients connections and then accept them when they come in. If a firewall is blocking incoming connections on that port, all incoming requests will not make it to your server!

So for example say you want your server to bind on port 6969, and you ran the server on your friends PC...

Your going to have to unblock ALL firewalls, That means logging into the router (if they have one) and setting up port forwarding so that any incoming TCP requests on port 6969 get forwarded to the computer running the server...

For me I only have/use a hardware firewall (built into my router) so to open a port for me that's all I'd have to do... The trend however with many people is using software firewalls too!

At my friends house when I wanted to test some of my client->server apps I had to not only configure his router, but also windows firewall and another firewall with his AV (one that I almost didn't find)

So make sure all firewalls are unblocked, if you don't want to disable the wall completely, then just add an exception ;)
Steve8x
New User
New User
 
Posts: 1
Joined: Mon Nov 03, 2008 9:27 pm
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by eljonto on Tue Nov 04, 2008 1:31 am
([msg=14450]see Re: TCP sockets in Python question[/msg])

ahhhh thanks!
one of my friends said on his comp a windows message came up saying something like "do you want to keep blocking server etc"- but i didn't think of the port forwarding- i guess ill have to find out how to add exceptions via software and so on- any ideas or suggestions please post back here, but thanks for your help.

EDIT: o yea- and i did get them to run server first- and even if they didn't with the original client code waits for the server to start if the client was started first anyway ;)
-Quis custodiet ipsos custodes?, Juvenal
_________________________________________________________________
User avatar
eljonto
Poster
Poster
 
Posts: 373
Joined: Thu Apr 17, 2008 1:16 am
Location: Australia
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by tgoe on Wed Nov 05, 2008 11:03 pm
([msg=14550]see Re: TCP sockets in Python question[/msg])

If you don't want the user of your server to have to enable port forwarding manually, you can try using UPnP. The MiniUPnP C library can be built with an optional python module that you can use. Also, the python Twisted framework does UPnP.
User avatar
tgoe
Contributor
Contributor
 
Posts: 633
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: TCP sockets in Python question

Post by eljonto on Thu Nov 06, 2008 2:42 am
([msg=14553]see Re: TCP sockets in Python question[/msg])

ah great thanks, will certainly try it out when i have the time
-Quis custodiet ipsos custodes?, Juvenal
_________________________________________________________________
User avatar
eljonto
Poster
Poster
 
Posts: 373
Joined: Thu Apr 17, 2008 1:16 am
Location: Australia
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests