我怎么知道服务器的IP地址?

时间:2016-05-01 05:19:05

标签: python pygame

我正在使用python(pygame和podsixnet)开发多人游戏。每次客户端必须连接到服务器时,都必须键入服务器的主机名和端口。我想通过自动向客户提供此信息来绕过这个阶段。如何找到服务器的地址?该游戏是按照以下教程开发的: https://www.raywenderlich.com/38732/multiplayer-game-programming-for-teens-with-python

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"

1 个答案:

答案 0 :(得分:0)

我也喜欢制作maltiplayer游戏! :)

这主要是您可能正在寻找 socket.gethostbyname(主机名)

一个完整的小脚本看起来像这样:

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

希望这有帮助!