与.onion域的服务器到服务器套接字连接 我想用python在tor上创建p2p连接,并直接与终端或使用python脚本连接。

与.onion域的服务器到服务器套接字连接 我想用python在tor上创建p2p连接,并直接与终端或使用python脚本连接。,python,python-3.x,sockets,p2p,tor,Python,Python 3.x,Sockets,P2p,Tor,为了实现这一点,我配置了一个隐藏服务,创建一个套接字服务器并绑定到隐藏服务端口。但它不起作用 这是我的服务器文件 #!/usr/bin/python #server.py import socket, sys, _thread, os from time import sleep import colorama from colorama import Fore, Back colorama.init(autoreset=True) os.system('cls' if os.name

为了实现这一点,我配置了一个隐藏服务,创建一个套接字服务器并绑定到隐藏服务端口。但它不起作用

这是我的服务器文件

#!/usr/bin/python
#server.py

import socket, sys, _thread, os
from time import sleep
import colorama
from colorama import Fore, Back

colorama.init(autoreset=True)


os.system('cls' if os.name == 'nt' else 'clear')

name = 'Server'


print('Setup Server...')

soc = socket.socket()
host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)
port = 1234
soc.bind((host_name, port))
print(host_name, '({})'.format(ip))


# name = input('Enter name: ')
soc.listen(1) #Try to locate using socket
print('Waiting for incoming connections...')
connection, addr = soc.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")
print('Connection Established. Connected From: {}, ({})'.format(addr[0], addr[0]))

client_name = connection.recv(1024)
client_name = client_name.decode()

os.system('cls' if os.name == 'nt' else 'clear')

print(client_name + ' has connected...')
print('Enter -q to exit.')
connection.send(name.encode())

def print_msg( thread_name ):
    while True:
        msg = connection.recv(1024)
        msg = msg.decode()
        if msg == "-q":
            print(f'{client_name} exit the chat room')
        elif msg != "":
            print(Fore.GREEN + f'{client_name}: {msg}')
        sleep(0.5)

while True:
    try:
        _thread.start_new_thread( print_msg, ("Thread-1", ) )
    except:
        break
    
    try:
        message = input()
        if message == '-q':
            connection.send(message.encode())
            print("\n")
            break
        connection.send(message.encode())
    except KeyboardInterrupt:
        print('Enter -q to close the connection.')

我的客户文件

#!/usr/bin/python3
#client.py

import socket, sys, _thread, os
from time import sleep
import socks

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)

os.system('cls' if os.name == 'nt' else 'clear')

remote_host = 'domain_name.onion'
name = 'Rudransh'


print('Client Server...')

soc = socket.socket()

port = 1234

sleep(1)
soc.connect((remote_host, port))
print("Connected...\n")

sleep(2)
os.system('cls' if os.name == 'nt' else 'clear')

soc.send(name.encode())
server_name = soc.recv(1024)
server_name = server_name.decode()
print(f'{server_name} has joined...')
print('Enter -q to exit.')

def print_msg( thread_name ):
    while True:
        msg = soc.recv(1024)
        msg = msg.decode()
        if msg == "-q":
            print(f'{server_name} close the connection')
        elif msg != "":
            print(f'{server_name}: {msg}')
        sleep(0.5)

while True:
    try:
        _thread.start_new_thread( print_msg, ("Thread-1", ) )
    except:
        break
    try:
        message = input()
        if message == "-q":
            soc.send(message.encode())
            print("\n")
            break
        soc.send(message.encode())
    except KeyboardInterrupt:
        print('Enter -q to close the connection.')
如果我使用本地IP通过同一个WiFi或LAN连接连接,它就会工作。 我想通过tor连接它