954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get the IP of the connected socket on server

Hi,

I got a server with several of IP's at. Those IP's I want to create sockets through and afterwards from a client, and afterwards send a socket to a webpage.
The problem is that I want to use the IP that the client is calling to create the socket to the webpage, and not just the main IP of the server.

Etc, I connect from client to:
xx.xx.xx.45
and then I create a socket to a webserver using,
s.bind((gethostname(), 1700))
s.connect(addr)
but that binding will attach it to the main IP: xx.xx.xx.12 and not xx.xx.xx.45 which is the IP I wans to bind to the webserver socket.

Is there any solution of how I can recall the IP that I created the socket through so I not just get the main IP of the server?

Xeex
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

gethostbyname / gethostbyaddress will always provide the main ip. That is if the sever is a shared hosting type.

Why not simply provide the ip address you want to bind to?

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

I have like 150+ different IP's I want to use and create sockets through. If I have to provide it everytime there will be a lot of hardcoding.
Would do it much easier if there is a way to recall the IP that the server was called at.

Xeex
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

why hard coding?
you can loop through your ips without any issue.;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

How can the python script then know if its the right IP it is binding to the socket?

Xeex
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Maybe 'it is better to ask forgiveness than permission' ie try.,except

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Yeah but the problem aint that I dont know if the server can create the socket. So I dont see any reason to use try and except.

The problem is that I connect to etc. xx.xx.xx.42 and then I also wants to bind that IP to the socket. But I dont know how to recall that IP, when I etc use gethostbyadress or anything else I get the main IP of the server that is xx.xx.xx.12

Hope that cleared what I asked about :)

Xeex
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Are you with me my friend? just loop on the ips and try/except, very easy ;)

you can also make a list of your ips and work your way out if you know what i mean.
You can even give any number of threads per ip.

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

now i got your problem understood.

using socket mean you must have a socket receiver/connecter at the other end for sync,

can you actually tell us what you want to achieve and perharps we can help you with a simple way out?

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

Sure. I want to be able to send a socket message with from a PHP script on my webserver with a HTTP header to a specific IP on the server.
Then the IP I call in the socket needs needs to bind it self to the webserver and send the message it recieves from my php script.
So kind of a proxy where I can be able to use all the IP's on the server if just I am able to recall the right IP again :)

Xeex
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

I get you but it seems you are now confused. This is python forum.....

In all of this, you must have a running server to communicate with your sockets request.

Look , just write something dowm ok. Show us what you get so far ;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

Hey.

Im not confused at all..
This is a python related question.. I got a server, and so on, and also a python script for it.

I did solve the problem now.
I used asyncroned method with sockets, and used this example and rewrote it:
http://parijatmishra.wordpress.com/2008/01/04/writing-a-server-with-pythons-asyncore-module/

Xeex
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Hey.

Im not confused at all..
This is a python related question.. I got a server, and so on, and also a python script for it.

I did solve the problem now.
I used asyncroned method with sockets, and used this example and rewrote it:
http://parijatmishra.wordpress.com/2008/01/04/writing-a-server-with-pythons-asyncore-module/

Xeex
Newbie Poster
7 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Great i just checkup your link. very nice article.

But you can create the same stuff using threading and socket.
My problem is what are you trying to achieve? I am very curious.
;)

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: