简体   繁体   English

azure vm 上的 Python 套接字服务器

[英]Python socket server on azure vm

I have an azure vm running Linux (ubuntu 18.04) LTS and i have created a python socket server binding a socket with我有一个运行 Linux (ubuntu 18.04) LTS 的 azure vm 并且我创建了一个 python 套接字服务器绑定一个套接字

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

but when i try to connect from another script via但是当我尝试通过另一个脚本连接时

socket.connect(ip,10000)

the server reject the connection.服务器拒绝连接。

How do i solve this?我该如何解决这个问题?

I already tried to open the port on my wm with我已经尝试在我的 wm 上打开端口

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

But the server still refuse my connection.但是服务器仍然拒绝我的连接。

Client script:客户端脚本:

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()

Server script:服务器脚本:

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()

Please use 0.0.0.0 or the IP address of your Azure Ubuntu VM instead of localhost as server socket bind host.请使用您的 Azure Ubuntu VM 的0.0.0.0或 IP 地址而不是localhost作为服务器套接字绑定主机。

If you use localhost as host bind by socket server, then just only client on the same machine can connect to your server, because localhost means 127.0.0.1 which only allow client connect to the server IP address 127.0.0.1 .如果您使用localhost作为通过套接字服务器绑定的主机,那么只有同一台机器上的客户端可以连接到您的服务器,因为localhost表示127.0.0.1 ,它只允许客户端连接到服务器 IP 地址127.0.0.1

Meanwhile, you need to refer to the offical documents Tutorial: Create and manage Azure virtual networks for Linux virtual machines with the Azure CLI or How to open ports to a virtual machine with the Azure portal to add an inbound rule in the NSG setting of networking via Azure CLI or on portal. Meanwhile, you need to refer to the offical documents Tutorial: Create and manage Azure virtual networks for Linux virtual machines with the Azure CLI or How to open ports to a virtual machine with the Azure portal to add an inbound rule in the NSG setting of networking通过 Azure CLI 或门户网站。

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

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