Sockets with Tor and Python - python

Hello I am building an IM P2P client/server in Python and would like to use Tor as a proxy for it the server runs using Threads while the client runs at the same time
Does the server running on the local system need to listen to Tor? If so how do I do this? How do I make the client connect to a remote system using Tor?
I have searched for some examples but they lead to a library that seems to be really awkward to download
Here is the relevant server/client code
#Client Connection
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((str(self.CLIENT_HOST).strip(), self.CLIENT_PORT))
#Server
source_ip = ''
#socket.gethostbyname(socket.gethostname())
PORT = 9001
### Initialize socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((source_ip, PORT))
#
server_socket.listen(5)
Any ideas are greatly welcomed and appreciated

Related

What IP should i put in place of 'localhost' in this example for socket python

I am trying to build a client and server script to interact with each other using sockets, but I am confused on where to find the IP that I should put in place of 'localhost' in this example. Someone please help direct me on where to find the thing to put in place of it.
Server side:
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 8089))
serversocket.listen(5) # become a server socket, maximum 5 connections
while True:
connection, address = serversocket.accept()
Client Side:
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))

How to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py)

I want to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py). I don't actually know if I'm doing it right, or maybe what I did is not a good practice. How can I do that? I'm still a beginner in socket programming.
client.py(local computer)
import socket
ip = "<insert ip here>"
port = 9999
Message = "Hello, Server"
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.sendto(Message.encode('utf-8'), (ip, port))
server.py(pythonanywhere)
import socket
ip = "<insert ip here>"
port = 9999
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSock.bind((ip, port))
while True:
data, addr = serverSock.recvfrom(4096)
print("Message: ", data)
You cannot run a udp socket server on PythonAnywhere. PythonAnywhere does not route arbitrary network traffic to the machines where you would be running the server code.

python socket does not connect to other networks

hi guys i'm studying socket in python, i'm having a hard time connecting with other machines
I have this simple code
import socket
host = ''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, 222))
print('wait...')
sock. listen(1)
conn, addr = sock.accept()
print('connected')
the code above is a server, I try to connect using this simple code
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.0.14', 222))
when I execute the client code, nothing happens, it is in an infinite wait, neither the server responds nor the client responds.
obs: this is my goal to connect to an external network on my network. The server code is running on another network, I want to connect to another network
I ran your code, and it's basically correct. But I don't think your port is perfect, it should be at least 1024.
You should make sure that the port(222) in firewall on your server computer is open.
I changed your port to 12345, it works on my computer.

How to exchange data on different networks using sockets?

I know this was already asked but the previous questions didn't help. I'm trying to send some data using sockets. Specifically I'm using my laptop as server and a Linux emulator (Termux) on my smartphone as a client. Here below you can see the two Python codes. For the server:
import socket
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
s.close()
And for the client:
import socket
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
s.close()
When I'm connected to the same WiFi and in HOST (in both codes) I put the IP I see from ipconfig (192.168.---.---) everything works. It also works if in the HOST of the server I put 0.0.0.0.
However, when I put the IP of the machine (that I can see on https://whatismyipaddress.com/) and instead of using the WiFi I use the phone connection I get: ConnectionRefusedError: [Errno 111] Connection Refused.
Can someone explain me how can I connect client and server when the networks are different? I have been stuck with this for a while.
I also tried to open a port on the Firewall following this procedure and put it in the code instead of 5555 but still it didn't work.
Thank you in advance for the help.

Issue with receiving response from 127.0.0.1 with udp client in python

I'm working through the book 'Black Hat Python' and am having issues with a UDP connection to 127.0.0.1 on port 80. I'm fairly new to this stuff so it's not something I can simply just sit down and troubleshoot quickly and efficiently, so I was hoping someone may be able to point out an error or maybe it's just my setup. I am testing this in Kali VM with VMware Fusion. The socket stays open and waits for a response, but never receives one.
import socket
target_host = "127.0.0.1"
taget_port = 80
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto("AAABBBCCC", (target_host, taget_port))
data, addr = client.recvfrom(4096)
print data
Your architecture of having a client and a server exist in the same thread is very unusual. I would not recommend it.
Nevertheless, your program will work the way you want if you .bind() the socket to the target address:
import socket
target_host = "127.0.0.1"
target_port = 8000
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.bind((target_host, target_port))
client.sendto("AAABBBCCC", (target_host, target_port))
data, addr = client.recvfrom(4096)
print data

Categories