问题描述
我想使用 python 通过 tor 创建 p2p 连接,并直接与终端或使用 python 脚本连接。
为了实现我配置了隐藏服务,创建了一个套接字服务器并绑定到隐藏服务端口。但是没有用。
这是我的服务器文件
#!/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,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,) )
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 连接它。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)