【问题标题】:Python socket server on azure vmazure vm 上的 Python 套接字服务器
【发布时间】:2019-11-19 22:10:11
【问题描述】:

我有一个运行 Linux (ubuntu 18.04) LTS 的 azure vm,并且我创建了一个 python 套接字服务器,用于绑定套接字

socket.bind(('localhost', 10000))

但是当我尝试通过另一个脚本连接时

socket.connect(ip,10000)

服务器拒绝连接。

我该如何解决?

我已经尝试在我的 wm 上打开端口

az vm open-port --resource-group myResourceGroup --name myVM --port 10000

但是服务器仍然拒绝我的连接。

客户端脚本:

import socket
import threading

class Client:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    def sendData(self):
        while True:
            self.sock.send(bytes(input("")))

    def __init__(self):
        self.sock.connect(('serverip', 10000))
        iThread = threading.Thread(target=self.sendData)
        iThread.daemon = True
        iThread.start()

        while True:
            data = self.sock.recv(1024)
            if not data:
                break
            print(data)

client = Client()

服务器脚本:

import socket
import threading
import random

def getuserdata(b):
    #function that works
    return a

class Server:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connections = []
    def __init__(self):
        self.sock.bind(('localhost', 10000))
        self.sock.listen(1)
        print (self.sock.getsockname())

    def handler(self, c , a):
        while True:
            try:
                data = c.recv(1024)
            except:
                self.connections.remove(c)
                c.close()
                print(str(a[0]) + ":" + str(a[1]), "disconnected")
                print("lobby has " + str(len(self.connections)) + " players connected")
                break
            #loop per client
            #for connection in self.connections:
            #    connection.send(data)

    def run(self):
        while True:
            if len(self.connections) < 8:
                c , a = self.sock.accept()
                cThread = threading.Thread(target=self.handler , args=(c,a))
                cThread.daemon = True
                cThread.start()
                self.connections.append(c)
                print(str(a[0]) + ":" + str(a[1]) , "connected")
                print("lobby has " + str(len(self.connections)) + " players connected")
            #loop del server
            else:
                break
        print("lobby piena")
        list= getuserdata(8)
        for index, player in enumerate(self.connections):
            player.send(bytes(list[index]))
        while True:
            pass

server = Server()
server.run()

【问题讨论】:

  • 你在创建socket的时候用的是哪个家族的?此外,您可能想改用socketserver。
  • 请添加客户端和服务器脚本以获取更多详细信息。
  • sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

标签: python azure sockets virtual-machine


【解决方案1】:

请使用0.0.0.0 或您的Azure Ubuntu VM 的IP 地址而不是localhost 作为服务器套接字绑定主机。

如果你使用localhost作为socket服务器绑定的主机,那么只有同一台机器上的客户端可以连接到你的服务器,因为localhost表示127.0.0.1只允许客户端连接到服务器IP地址@987654328 @。

同时,您需要参考官方文档Tutorial: Create and manage Azure virtual networks for Linux virtual machines with the Azure CLI或How to open ports to a virtual machine with the Azure portal通过Azure CLI或门户网站在网络的NSG设置中添加入站规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多