简体   繁体   English

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

[英]How can I know the ip address of the server?

I am developing a multiplayer game using python (pygame and podsixnet). 我正在使用python(pygame和podsixnet)开发多人游戏。 Every time the client has to connect to the server, it has to type the server's hostname and port. 每次客户端必须连接到服务器时,都必须键入服务器的主机名和端口。 I want to bypass this stage by automatically providing the client with this information. 我想通过自动向客户提供此信息来绕过这个阶段。 How can I find the address of my server? 如何找到服务器的地址? The game has been developed by following the tutorial: https://www.raywenderlich.com/38732/multiplayer-game-programming-for-teens-with-python 该游戏是按照以下教程开发的: 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"

I like making maltiplayer games too! 我也喜欢制作maltiplayer游戏! :) :)

This is mostly what you may be looking for socket.gethostbyname(hostname) 这主要是你可能正在寻找的socket.gethostbyname(hostname)

a full small script would look like this: 一个完整的小脚本看起来像这样:

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM