1
我正在開發一個使用python(pygame和podsixnet)的多人遊戲。每次客戶端連接到服務器時,都必須輸入服務器的主機名和端口。我想通過自動向客戶端提供這些信息來繞過這個階段。我怎樣才能找到我的服務器的地址?遊戲已經開發了教程如下: https://www.raywenderlich.com/38732/multiplayer-game-programming-for-teens-with-python如何知道服務器的IP地址?
def __init__(self):
self.justplaced=10
n = self.dialog()
print n
pygame.init()
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Scrabble')
self.gameid=None
self.num=None
self.lastx, self.lasty = 0, 0
self.mousex = 0 # used to store x coordinate of mouse event
self.mousey = 0 # used to store y coordinate of mouse event
#print BGCOLOR
self.screen.fill(BGCOLOR)
#pygame.display.update()
self.is_Placed = self.generateBoxData(False)
self.placed_Letter = self.generateBoxData("")
self.is_Formed = self.generateBoxData(False)
self.owner = self.generateBoxData(99)
self.is_Clicked = self.generateBoxData(False)
address=raw_input("Address of Server: ")
try:
if not address:
host, port="localhost", 8000
else:
host,port=address.split(":")
self.Connect(("localhost" ,int(port)))
except:
print "Error Connecting to Server"
print "Usage:", "host:port"
print "e.g.", "localhost:31425"
exit()
print "Boxes client started"
可能的重複[使用Python的stdlib查找本地IP地址](http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib) – tknickman