sockets, gethostname() changing

7stud bbxx789_05ss at yahoo.com
Fri May 25 12:57:29 EDT 2007


>For local testing it is *much* easier to have your client
>and server use IP address 127.0.0.1

According to my book, an address is a tuple of the form (hostname,
port), so I didn't know what you meant by using 127.0.0.1 as the
address.  I played around with it, and using the tuple ("127.0.0.1",
1234) for the address worked(the server and client were able to
communicate) and I got the expected output -- whether I was connected
to the internet or not.  I experimented a little more and "localhost"
also worked for the hostname, and when I did that this line:

c, addr = s.accept()

produced an addr that contained "127.0.0.1" for the hostname.

> Don't use any hostname at all; use '' instead. That should bind
> to any interfase on your computer.

That worked too when I was connected to the internet, and addr once
again contained "127.0.0.1" for the hostname.


> Looks like a DHCP configuration issue to me; one might try the
> remedies suggested at

I previously read similar postings about changing the HOSTNAME in /etc/
hostconfig from AUTOMATIC to a chosen name, but when I looked at /etc/
hostconfig there was no entry for HOSTNAME. Ok, I'll give it a
whirl...

I added the line:

HOSTNAME=my.comp

to /etc/hostconfig, and after restarting my computer, that succeeded
in making the prompt in Terminal stay the same whether I was connected
to the internet or not.  But I got these errors when I tried to run my
client/server programs:

not connected to internet:
---------------------------
Traceback (most recent call last):
  File "./programs_python/socketsServer.py", line 9, in ?
    s.bind((host, port))
  File "<string>", line 1, in bind
socket.gaierror: (7, 'No address associated with nodename')

connected to the internet:
-------------------------
Traceback (most recent call last):
  File "./programs_python/socketsServer.py", line 9, in ?
    s.bind((host, port))
  File "<string>", line 1, in bind
socket.error: (49, "Can't assign requested address")
-------------------

So I deleted the line:

HOSTNAME=my.comp

from the hostconfig file and restarted my computer.   After that my
client/server programs would run as before. But now I get different
output from my server program:

Got socket connection from ('127.0.0.1', 49222)

The strange thing is: the hostname and port in the output are not what
I'm using in my server program:
---------
import socket

s = socket.socket()

print "made changes 2"

host = socket.gethostname()  #I'm not connected to the internet when I
use this line
print host

port = 1291
s.bind((host, port))

s.listen(5)
while("Ctrl-C hasn't been entered"):
    c, addr = s.accept()  #blocks and waits for client connection
    print "Got socket connection from", addr
    c.send("Thank you for connecting.  Now get lost.")
    c.close()
----------

The full output of that program is:

made changes 2
my-names-computer.local
Got socket connection from ('127.0.0.1', 49222)

The hostname now appears to be permanently stuck as "127.0.0.1", and
the port is wrong. That output was so confusing to me, I wasn't even
sure whether the file I was editing was actually the file that was
executing, so I printed out "made changes #" at the top of the file to
make sure the file I was editing was the one that was actually
executing.

I can't remember exactly what the output was for addr before I started
messing around with the HOSTNAME in /etc/config, but I'm pretty sure
addr contained the same hostname as the line above it in the output,
and the port matched the port in the program.

Any ideas why the hostname and port in the last line of the output are
not the same as the ones used in the program anymore?











More information about the Python-list mailing list