【发布时间】:2011-02-24 12:47:35
【问题描述】:
我使用了 python 的 socket 模块并尝试使用打开一个监听套接字
import socket
import sys
def getServerSocket(host, port):
for r in socket.getaddrinfo(host, port, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = r
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.bind(sa)
s.listen(1)
except socket.error, msg:
s.close()
s = None
continue
break
if s is None:
print 'could not open socket'
sys.exit(1)
return s
其中主机为无,端口为 15000。
然后程序会接受连接,但只能来自同一台机器上的连接。我必须做什么才能接受来自互联网的连接?
【问题讨论】:
标签: python networking sockets network-programming