What's the wrong with udp code in Python? - python

I hope to receive 300*3 = 900 of messages at "192.168.1.114" in multi thread case, but 876 messages arrived. however, it can all arrived by single thread. and it also can all arrived by sendto '127.0.0.1'.
Is it because of the reasons for UDP?
import socket
import threading
from time import sleep
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = '192.168.1.114'
port = 9999
def sendMsg(args):
msg = 'thread %06d - 1' % args
s.sendto(msg, (host, port))
sleep(1)
msg = 'thread %06d - 2' % args
s.sendto(msg, (host, port))
sleep(1)
msg = 'thread %06d - 3' % args
s.sendto(msg, (host, port))
threads = []
for i in range(300):
t = threading.Thread(target=sendMsg, args=(i,))
threads.append(t)
t.start()
for j in threads:
j.join()
s.close()

Related

Getting a TimeOut Error when trying to run a python code that transfers files from EC2 to Local

This is the code on the AWS EC2 instance:
import socket
from threading import Thread
from socketserver import ThreadingMixIn
TCP_IP = 'localhost'
TCP_PORT = 9001
BUFFER_SIZE = 1024
class ClientThread(Thread):
def __init__(self,ip,port,sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print (" New thread started for "+ip+":"+str(port))
def run(self):
filename='mytext.txt'
f = open(filename,'rb')
while True:
l = f.read(BUFFER_SIZE)
while (l):
self.sock.send(l)
#print('Sent ',repr(l))
l = f.read(BUFFER_SIZE)
if not l:
f.close()
self.sock.close()
break
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpsock.listen(5)
print ("Waiting for incoming connections...")
(conn, (ip,port)) = tcpsock.accept()
print ('Got connection from ', (ip,port))
newthread = ClientThread(ip,port,conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
And this is the code on my local machine:
import socket
import time
#TCP_IP = 'localhost'
TCP_IP = 'ip-ec2-instance'
TCP_PORT = 60001
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
clock_start = time.clock()
time_start = time.time()
with open('received_file', 'wb') as f:
print ('file opened')
while True:
#print('receiving data...')
data = s.recv(1024)
#print('data=%s', (data))
if not data:
f.close()
print ('file close()')
break
# write data to a file
f.write(data)
print('Successfully get the file')
s.close()
print('connection closed')
clock_end = time.clock()
time_end = time.time()
duration_clock = clock_end - clock_start
print ('clock: start = ',clock_start, ' end = ',clock_end)
print ('clock: duration_clock = ', duration_clock)
duration_time = time_end - time_start
print ('time: start = ',time_start, ' end = ',time_end)
print ('time: duration_time = ', duration_time)
Now, the code on the EC2 instance seems to run fine and waits for a connection, but the code on my local machine gives me the error mentioned below when I try running it with the public IP of the EC2 instance:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I can't figure out how to fix this.

Connection error between local machine and AWS EC2 instance through python code

When I run the following code, I get an error saying :
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
# server3.py on EC2 instance
import socket
from threading import Thread
from SocketServer import ThreadingMixIn
# TCP_IP = socket.gethostbyaddr("your-ec2-public_ip")[0]
TCP_IP = socket.gethostbyaddr("3.129.88.101")[0]
TCP_PORT = 60001
BUFFER_SIZE = 1024
print 'TCP_IP=',TCP_IP
print 'TCP_PORT=',TCP_PORT
class ClientThread(Thread):
def __init__(self,ip,port,sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print " New thread started for "+ip+":"+str(port)
def run(self):
filename='mytext.txt'
f = open(filename,'rb')
while True:
l = f.read(BUFFER_SIZE)
while (l):
self.sock.send(l)
#print('Sent ',repr(l))
l = f.read(BUFFER_SIZE)
if not l:
f.close()
self.sock.close()
break
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpsock.listen(5)
print "Waiting for incoming connections..."
(conn, (ip,port)) = tcpsock.accept()
print 'Got connection from ', (ip,port)
newthread = ClientThread(ip,port,conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
# client3.py on local machine
#!/usr/bin/env python
#!/usr/bin/env python
import socket
import time
#TCP_IP = 'ip-ec2-instance'
TCP_IP = '3.129.88.101'
TCP_PORT = 60001
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
clock_start = time.perf_counter()
time_start = time.time()
with open('received_file', 'wb') as f:
print 'file opened'
while True:
#print('receiving data...')
data = s.recv(1024)
#print('data=%s', (data))
if not data:
f.close()
print 'file close()'
break
# write data to a file
f.write(data)
print('Successfully get the file')
s.close()
print('connection closed')
clock_end = time.clock()
time_end = time.time()
duration_clock = clock_end - clock_start
print 'clock: start = ',clock_start, ' end = ',clock_end
print 'clock: duration_clock = ', duration_clock
duration_time = time_end - time_start
print 'time: start = ',time_start, ' end = ',time_end
print 'time: duration_time = ', duration_time
I have no idea what I am doing wrong and it would be helpful if someone could help.

python socket listener not receiving data

I am programming a decentralised script to track the IPs of other computers running the script, to explore decentralisation. This script isolates the problem. The code consists of 2 scripts, one main program which sends its IP to an IP provided if one is provided, and a listener program which is run as a subscript and listens for data and pipes that data back to the main program. The main script appears to be working, the data is sent over the network, but the listener does not receive it.
This is the main script
import socket
from subprocess import Popen, PIPE
from time import sleep
def getIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.4', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
def sendfyi(target, ownIP):
toSend = 'fyi' + ':' + ownIP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((target, 50000))
s.send(toSend.encode())
s.close()
print('sent fyi')
otherIPs = []
ownIP = getIP()
targetIP = input('enter ip or 0: ')
if targetIP != '0':
otherIPs.append(targetIP)
sendfyi(targetIP, ownIP)
listener = Popen(['python3', 'testlistener.py'], stdout=PIPE, stderr=PIPE)
i = 0
while i == 0:
sleep(1)
listenerPipe = listener.stdout.readline()
print(listenerPipe)
This is the sub process:
import socket
def getIP():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
s.connect(('8.8.8.4', 1))
IP = s.getsockname()[0]
except Exception:
IP = '127.0.0.1'
finally:
s.close()
return IP
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((getIP(), 50000))
i = 1
while i == 1:
s.listen(1)
conn, addr = s.accept()
print('conected', flush=True)
data = conn.recv(1024)
print('data receved', flush=True)
out = data.decode()
print('data decoded', flush=True)
print(out, flush=True)
conn.close()
Incorect bind statement
bind(('', 50000))

Python tcp server with multithreading can not print return value from thread

I am trying to set up a tcp server in Python using Multithreading. The problem is that the queue only return data from only one thread. That means it only prints the data from the thread created with 5555 or 5556. I confirmed both data reaches port by connecting one device per time but it does not print the result from connection to the other port when more than one device connected.
Thanks for your help.
#!usr/bin/python
from threading import Thread
import socket
import sys
from queue import Queue
def clientthread(conn,q):
buffer=""
data = conn.recv(8192)
buffer=data
q.put(buffer)
return buffer
def main():
try:
host = '169.254.162.144'
port = 5555
tot_socket =2
list_sock = []
conarr=[]
threads=[]
for i in range(tot_socket):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host, port+i))
s.listen(10)
list_sock.append(s)
print ("[*] Server listening on %s %d" %(host, (port+i)))
for j in range(len(list_sock)):
conn, addr = list_sock[j].accept()
conarr.append(conn)
print('[*] Connected with ' + addr[0] + ':' + str(addr[1]))
while 1:
queue1 = Queue()
for j in range(len(list_sock)):
thread =Thread( target=clientthread, args=(conn, queue1) )
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
while not queue1.empty():
result = queue1.get()
print(result)
threads=[]
s.close()
except KeyboardInterrupt as msg:
sys.exit(0)
if __name__ == "__main__":
main()

control not returned to main thread

I have written a python program in which I created a UDP listener in thread 1 and trying to do something in thread 2. The issue here is control is stuck up in thread 1 and it never returns to main thread so thread 2 is not even starting up.
import threading
import socket
import time
data = ''
def ListenerUDP():
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address1 = ('localhost', 5000)
print('starting up UDP server on %s port %s' % server_address1)
sock1.bind(server_address1)
while True:
print('\nUDP server is now listening up')
data, address = sock1.recvfrom(4096)
print('received %s bytes from %s' % (len(data), address))
print('is %s' % data.hex())
def Forwarder():
print('do something')
print('Starting main thread')
t1 = threading.Thread(target=ListenerUDP)
t1.start()
t2 = threading.Thread(target=Forwarder)
t2.start()
can someone please help fixing it.

Categories