Python - 1 program, send and receive on different ports, but the same host, is this possible?

With Python, is it possible to send UDP data to a local host and some port, and then simultaneously inside the same program, listen to another port on the local host? I keep getting the 48'address already in use patch and tried to use the python reuse address, although I am sure that it will not work for this application in any case.

Background: I don't know anything about software development much less than Python, it's just what someone asked for at work.

I appreciate any help.

from threading import Thread import time import socket HOST = 'localhost' PORT = 5455 PORT1 = 5457 data1 = "1" s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((HOST,PORT1)) a = 0 def myfunction(string, *args): while 1: cmd = int( raw_input("send message: ") ) if (cmd == 1): s.sendto(data1, (HOST,PORT)) time.sleep(1) def myfunction2(string, *args): while 1: print s.recv(30) time.sleep(.5) if __name__=='__main__': try: Thread(target=myfunction, args=(a, 1)).start() Thread(target=myfunction2, args=(a, 1)).start() except Exception, errtxt: print errtxt 
+4
source share
2 answers

Yes it is. In any language. You are probably listening to the same port twice; TCP and UDP endpoints are characterized by IP address and port. "Address already in use" will only be displayed for full compliance, the same address and the same port.

Also make sure the listening port is not yet used with netstat .

UPDATE (thanks to l4mpi): you will get "access denied" if you try to use a port below 1024 without superuser privileges.

UPDATE

I changed your code a bit; one of the problems you encountered was some confusion regarding sending and receiving sockets, which was a “client” function and which was a “server”.

I took the liberty of requesting the body of the message instead of "1", but if necessary it is easy to return things.

 from threading import Thread import time import socket CONN = ('localhost', 5455) def fn_client(string, *args): cs = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) cs.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) while 1: cmd = int( raw_input("command (1 to send): ") ) if (cmd == 1): data = raw_input("message to send: ") cs.sendto(data, CONN) time.sleep(1) def fn_server(string, *args): ss = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ss.bind(CONN) while 1: print "Server received '%s'" % (ss.recv(30)) time.sleep(.5) if __name__=='__main__': a = 0 try: Thread(target=fn_client, args=(a, 1)).start() Thread(target=fn_server, args=(a, 1)).start() except Exception, errtxt: print errtxt 
+5
source

Your code works for me if it does not generate Address already in use .

But your stream code is not too clean, KeyboardInterrupt not processed in streams. This is a common problem with multithreading, see this answer or this recipe for an example on how to reduce it.

This means that you cannot gracefully terminate your program using CTRL-C . Instead, you probably had to resort to using something like kill [pid] , maybe even with -9 ? I assume that you got the remaining connections from previous runs of the program that called Address already in use . Use something like netstat -anp | grep 5457 netstat -anp | grep 5457 to determine if there are more connections on this port.

Also see Doug Hellman's article for a good introduction to streams.

+1
source

Source: https://habr.com/ru/post/1435347/


All Articles