2016-05-01 64 views
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" 
+0

可能的重复[使用Python的stdlib查找本地IP地址](http://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib) – tknickman

回答

0

我喜欢做maltiplayer游戏呢! :)

这主要是什么,你可能会寻找socket.gethostbyname(主机名)

一个完整的小脚本应该是这样的:

import socket 
hostname = 'maps.google.com' 
addr = socket.gethostbyname(hostname) 
print 'The address of ', hostname, 'is', addr 

希望这有助于!