• 10 dec 2017: forum version update. In case of issues use this topic.
  • 30 nov 2017: pilight moved servers. In case of issues use this topic.
Hello There, Guest! Login Register


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Possible problem leaving ports open when connecting with python
#1
I'm using a bit of Python to fetch values from pilight.
All works well, but something pilight sort of stops.
I've increased logging and found that for each connect a new port is used
Code:
[Jun 10 09:36:03:362653] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57760
[Jun 10 09:37:02:605209] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57764
[Jun 10 09:38:02:856995] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57768
[Jun 10 09:39:03:915571] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57772
[Jun 10 09:40:03:142001] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57776
[Jun 10 09:41:03:355453] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57780
[Jun 10 09:42:02:589662] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57784
[Jun 10 09:43:02:850246] pilight-daemon: INFO: new client, ip: 192.168.178.11, port: 57788

Python script retrieves data every minute
- There are no INFO lines of connections closing
- New ports are used.
- How many ports can be opened simultaneously before creating problems?

Should i somehow terminate the connection?
Piece of python below


Code:
responses = discover("urn:schemas-upnp-org:service:pilight:1");
if len(responses) > 0:
    locationsrc = re.search('Location:([0-9.]+):(.*)', str(responses[0]), re.IGNORECASE)
    if locationsrc:
        location = locationsrc.group(1)
        port = locationsrc.group(2)

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket.setdefaulttimeout(0)
    s.connect((location, int(port)))
    s.send('{"action":"identify","options":{"receiver":1}}\n')
    text = "";    
    while True:
        line = s.recv(1024)
        text += line;        
        if "\n\n" in line[-2:]:
            text = text[:-2];            
            break;
        if "\r\n" in line[-2:]:
            text = text[:-2];            
            break;
        if "\n\r" in line[-2:]:
            text = text[:-2];            
            break;    
        if "\n" in line[-1:]:
            text = text[:-1];                
            break;
        if "\r" in line[-1:]:
            text = text[:-1];                
            break;
            
    if text == '{"status":"success"}':
        text = ""        
        s.send('{"action": "request values"}\n')
        
        while True:
            line = s.recv(1024)
            
            text += line;
            if "\n\n" in line[-2:]:
                text = text[:-2];
                break;
            if "\r\n" in line[-2:]:
                text = text[:-2];            
                break;
            if "\n\r" in line[-2:]:
                text = text[:-2];            
                break;                
            if "\n" in line[-1:]:
                text = text[:-1];                
                break;    
            if "\r" in line[-1:]:
                text = text[:-1];                
                break;
    s.close()
 
Reply
#2
The s.close() line at the end terminates the socket in your script, so you don't need to worry about that.
IIRC, you can have up to 65,536 ports on a host, but once s.close() is called it will free up the ones used by your python script. These can then be used again.
It's unlikely that you will run into any kind of issue due to using a new port for every connection, as the ports will be closed and released so that they can be used again.
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  Problem with TOP-201 webcam Ulrich.Arnold 4 1,468 05-22-2017, 04:09 PM
Last Post: Ulrich.Arnold
  Reset of GPIO ports / Detection of mobile phone NormBot 1 1,349 09-02-2016, 08:30 AM
Last Post: wo_rasp
  Problem with DATE_ADD fleisch 1 1,245 03-28-2016, 02:48 PM
Last Post: Niek
  Problem on config.json aXpi 14 6,798 10-14-2015, 09:13 PM
Last Post: curlymo
  Execute python script from Generic Switch? panton 4 3,323 04-28-2015, 07:58 AM
Last Post: curlymo
  problem with pthread_setschedparam() martinr 1 2,165 02-04-2015, 12:02 PM
Last Post: curlymo
  Little Problem finding files suchalaugh 9 3,394 01-09-2015, 11:35 PM
Last Post: suchalaugh
  dht22 with wiringPi Problem splaxter 16 12,648 12-15-2014, 11:40 PM
Last Post: curlymo
  Problem with universal command cybersyx 0 1,242 11-04-2014, 03:26 PM
Last Post: cybersyx
  elro problem Bram 80 26,365 01-23-2014, 06:23 PM
Last Post: dmnkhhn

Forum Jump:


Browsing: 1 Guest(s)