套接字,gethostname()更改 [英] sockets, gethostname() changing

查看:56
本文介绍了套接字,gethostname()更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试一个基本的套接字程序(来自一本书),并且客户端和服务器程序都打开了

我的电脑。在这两个程序中,

我调用了socket.gethostname(),但是我发现当我连接到互联网时,无论是客户端还是服务器,都没有发生任何事情。

我发现当我连接到互联网时,我的计算机的主机名会自动更改为我的isp的主机名,并且可能是
$ b我的计算机上的$ b服务器程序无法监听我的isp'的地址(主机,

端口)。有没有办法让我的计算机的主机名保持静态,所以

当我连接到

互联网时它不会改为我的isp'的主机名。我正在使用mac os 10.4.7。为什么我的电脑的主机名

首先会动态改变?


服务器程序:

---- ---------------

进口插座

s = socket.socket()

host = socket.gethostname()

打印主机

port = 1274

s.bind((主机,端口) )


s.listen(5)

while(Ctrl-C还没输入):

c,addr = s.accept()#blocks并等待客户端连接

print"来自的套接字连接,addr

c.send("谢谢连接。现在迷路了。)

c.close()

客户端程序:

-------- -----------

导入套接字


s = socket.socket()


host = socket.gethostname()

port = 1274

s.connect((主持人,端口))

打印s.recv(1024)

s.close()

解决方案

< blockquote> 5月24日晚上8点04分,7stud< bbxx789_0 ... @ yahoo.comwrote:





我正在尝试一个基本的套接字程序(来自一本书),并且客户端和服务器程序都在我的计算机上。在这两个程序中,

我调用了socket.gethostname(),但是我发现当我连接到互联网时,无论是客户端还是服务器,都没有发生任何事情。

我发现当我连接到互联网时,我的计算机的主机名会自动更改为我的isp的主机名,并且可能是
$ b我的计算机上的$ b服务器程序无法监听我的isp'的地址(主机,

端口)。有没有办法让我的计算机的主机名保持静态,所以

当我连接到

互联网时它不会改为我的isp'的主机名。我正在使用mac os 10.4.7。为什么我的电脑的主机名

首先会动态改变?


服务器程序:

---- ---------------

进口插座

s = socket.socket()

host = socket.gethostname()

打印主机

port = 1274

s.bind((主机,端口) )


s.listen(5)

while(Ctrl-C还没输入):

c,addr = s.accept()#blocks并等待客户端连接

print"来自的套接字连接,addr

c.send("谢谢连接。现在迷路。)

c.close()


客户端程序:

---- ---------------

进口插座

s = socket.socket()

host = socket.gethostname()

port = 1274

s.connect((主机,端口))

print s.recv(1024)

s.close()



我可以'想象一下为什么你的主机名会改变,除非你安装了一些他们的专有软件,而这些软件都是用b / b
的东西搞砸了。在Sys Prefs-> Sharing中设置的主机名是什么?尝试

设置它。什么是之前和之后的连接名称

得到什么?


~Sean


< blockquote>感谢您的回复。


5月24日晚9点24分,half.ital ... @ gmail.com写道:


我无法想象为什么你的主机名会改变,除非你安装了一些他们的专有软件,而不是用b / b
的东西。



当我第一次使用终端时,我注意到当我连接到互联网时,终端中的提示已经改变了。


在Sys Prefs-> Sharing中设置的主机名是什么?



我姓名的电脑


尝试

设置它。什么是之前和之后的连接名称

get?



如果我添加以下行:


host = socket.gethostname()

打印主机#< ----------


和我是没有连接到互联网,我运行程序,我得到:


my-names-computer.local


当我在连接到互联网,我得到:


dialup-9.999.999.999.dial9.xxxxxxx.level9.net


< blockquote> 7stud写道:





我正在尝试一个基本的套接字程序(来自一本书) ,以及客户端和服务器程序都在我的计算机上的
。在这两个程序中,

我调用了socket.gethostname(),但是我发现当我连接到互联网时,无论是客户端还是服务器,都没有发生任何事情。

我发现当我连接到互联网时,我的计算机的主机名会自动更改为我的isp的主机名,并且可能是
$ b我的计算机上的$ b服务器程序无法监听我的isp'的地址(主机,

端口)。有没有办法让我的计算机的主机名保持静态,所以

当我连接到

互联网时它不会改为我的isp'的主机名。我正在使用mac os 10.4.7。为什么我的电脑的主机名

首先会动态改变?


服务器程序:

---- ---------------

进口插座

s = socket.socket()

host = socket.gethostname()

打印主机

port = 1274

s.bind((主机,端口) )


s.listen(5)

while(Ctrl-C还没输入):

c,addr = s.accept()#blocks并等待客户端连接

print"来自的套接字连接,addr

c.send("谢谢连接。现在迷路了。)

c.close()


客户端程序:

---- ---------------

进口插座

s = socket.socket()

host = socket.gethostname()

port = 1274

s.connect((主机,端口))

print s.recv(1024)

s.close()



for loc测试它是* *更容易让你的客户端和服务器使用

IP地址127.0.0.1 - 这是loopback

网络上的通常地址,这不需要任何物理网络硬件来运行。


就感兴趣而言,socket.gethostname()返回的是什么?我怀疑这将取决于你是否已经建立了一个界面

特定的域名后缀 - 我没有,我的代码没有问题。


问候

Steve

-

Steve Holden +1 571 484 6266 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://del.icio。我们/ steve.holden

------------------ Asciimercial -------------- -------

上网:博客,镜头和标记你的名气!!

holdenweb.blogspot.com squidoo.com/pythonology

标记物品:del.icio.us/steve.holden/python

所有这些服务目前都提供免费注册!

----- ---------感谢您的阅读----------------


Hi,

I''m experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I''m connected to the internet, and presumably the
server program on my computer cannot listen on my isp''s address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn''t change to my isp''s hostname when I connect to the
internet. I''m using mac os 10.4.7. Why does my computer''s hostname
dynamically change in the first place?

server program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
print host
port = 1274
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()
client program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
port = 1274

s.connect((host, port))
print s.recv(1024)
s.close()

解决方案

On May 24, 8:04 pm, 7stud <bbxx789_0...@yahoo.comwrote:

Hi,

I''m experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I''m connected to the internet, and presumably the
server program on my computer cannot listen on my isp''s address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn''t change to my isp''s hostname when I connect to the
internet. I''m using mac os 10.4.7. Why does my computer''s hostname
dynamically change in the first place?

server program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
print host
port = 1274
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()

client program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
port = 1274

s.connect((host, port))
print s.recv(1024)
s.close()

I can''t imagine why your hostname would be changing, unless you
installed some of their proprietary software thats messing around with
things. What is the hostname set to in Sys Prefs->Sharing? Try
setting it there. What are the before and after connection names you
get?

~Sean


Thanks for the response.

On May 24, 9:24 pm, half.ital...@gmail.com wrote:

I can''t imagine why your hostname would be changing, unless you
installed some of their proprietary software thats messing around with
things.

When I first started using Terminal, I noticed that the prompt in
Terminal changed when I was connected to the internet.

What is the hostname set to in Sys Prefs->Sharing?

My Name''s Computer

Try
setting it there. What are the before and after connection names you
get?

If I add the line:

host = socket.gethostname()
print host #<----------

and I''m not connected to the internet and I run the program, I get:

my-names-computer.local

When I''m connected to the internet, I get:

dialup-9.999.999.999.dial9.xxxxxxx.level9.net


7stud wrote:

Hi,

I''m experimenting with a basic socket program(from a book), and both
the client and server programs are on my computer. In both programs,
I call socket.gethostname(), but I discovered that when I am connected
to the internet, both the client and server hang and nothing happens.
I discovered that the hostname of my computer automatically changes to
that of my isp when I''m connected to the internet, and presumably the
server program on my computer cannot listen on my isp''s address(host,
port). Is there a way to make the hostname of my computer static, so
that it doesn''t change to my isp''s hostname when I connect to the
internet. I''m using mac os 10.4.7. Why does my computer''s hostname
dynamically change in the first place?

server program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
print host
port = 1274
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()
client program:
-------------------
import socket

s = socket.socket()

host = socket.gethostname()
port = 1274

s.connect((host, port))
print s.recv(1024)
s.close()

For local testing it is *much* easier to have your client and server use
IP address 127.0.0.1 - this is the usual address on the "loopback"
network, which doesn''t require any physical network hardware to operate.

Just as a matter of interest, what is socket.gethostname() returning? I
suspect it will depend on whether you have established an interface
specific domain suffix - I haven''t, and I have no trouble with your code.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------


这篇关于套接字,gethostname()更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆