Python Forum
Pythonosc- turn on server for 10 minutes and close in a loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Pythonosc- turn on server for 10 minutes and close in a loop (/thread-9079.html)



Pythonosc- turn on server for 10 minutes and close in a loop - SwethaSharma - Mar-20-2018

Hi Everyone,


I am using python osc for connecting to a sensor, I want to connect to the sensor for 10 minutes receive the messages and close the server. The following is the code I have for doing job:


from pythonosc import udp_client
from pythonosc import osc_message_builder
from pythonosc import dispatcher
import socket, threading, time
from pythonosc import osc_server
import time

tstart=time.time();
# Change this to the NGIMU IP address
send_address = '192.100.1.1', 9000


client =udp_client.SimpleUDPClient('192.100.1.1', 9000)

# msg=osc_message_builder.OscMessageBuilder()
# msg=msg.build()
print(str(socket.gethostbyname(socket.gethostname())))

#receive_address='192.100.16.2', 8000
dispatcher = dispatcher.Dispatcher()

server = osc_server.ThreadingOSCUDPServer(('192.100.16.2', 8001),dispatcher)
#server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
print("Serving on {}".format(server.server_address))

server.server_activate()
#server.server_close()

tnow= time.time();
tdiff= tnow-tstart
while(tdiff*1000<10):
    tnow= time.time();
    tdiff= tnow-tstart;
    print(tdiff)
    if(tdiff*1000>10):
       server.server_close()
I am getting the following error:

Error:
File "<ipython-input-3-dbc750281963>", line 1, in <module> runfile('C:/sensor/ngimu.py', wdir='C:/sensor/') File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace) File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/sensor/ngimu.py", line 27, in <module> server = osc_server.ThreadingOSCUDPServer(('192.168.1.2', 8001),dispatcher) File "C:\ProgramData\Anaconda3\lib\site-packages\pythonosc\osc_server.py", line 107, in __init__ super().__init__(server_address, _UDPHandler) File "C:\ProgramData\Anaconda3\lib\socketserver.py", line 453, in __init__ self.server_bind() File "C:\ProgramData\Anaconda3\lib\socketserver.py", line 467, in server_bind self.socket.bind(self.server_address) OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
How do I solve this error?
-swetha