网站无法连接到我的计算机

2024-04-24 09:25:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个游戏。你知道吗

在这里找到:https://docs.google.com/document/d/1zxC9Mo9LtcMf3jXA3z4xdz7Lj3G3EVGA4_CQzjoeyOA/edit?usp=sharing

我正试着让它成为一个新的加入时,我得到一个信息。我当前的方法是让站点通过某个端口连接到我的计算机,然后触发以下python脚本:

import discord
import asyncio
import websockets
import threading
f = open('runs.txt', 'r')
runs = int(f.readline())
f.close()
def write(input):
    f = open( 'runs.txt', 'w' )
    f.write( str(input) )
    f.close()
username = '<Bot Username>'
password = '<Bot Password>'
client = discord.Client()
client.login(username, password)
chan =  discord.PrivateChannel('<Username>', <Channel ID>)
@asyncio.coroutine
def hello(websocket, path):
    global runs
    input = yield from websocket.recv()
    print(input)
    if input == 'started':
        runs += 1
        write(runs)
        client.send_message(chan, 'User has played Daedalus')
start_server = websockets.serve(hello, '<My public IP>', <Forwarded Port>)


@client.event
def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')


@client.event
def on_message(message):
    if message.content.lower().startswith('check'):
        client.send_message(message.author, 'There are now '+ str(runs) + ' computers that have played daedalus')


def asyncBegin():
    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()

disco = threading.Thread(target=client.run)
disco.start()

asy = threading.Thread(target=asyncBegin())
asy.start()

然后,这个程序使用在线应用Discord通过我创建的bot向我发送消息。提到的文本文档只是为了保留运行次数。在游戏中,它被编程为立即连接到我的公共IP与正确的端口号,这应该运行程序。你知道吗

我发现在localhost上它可以正常工作,但是只要我尝试通过web进行连接就不起作用了。你知道吗

当我查看连接状态时,它只是在关闭连接之前尝试连接一段时间。我没有任何防火墙或网络安全,而我的测试。是什么导致它断开连接?你知道吗


Tags: runimportclienteventasyncio游戏messageinput