Hi all!
I'm a newbie to Python with my background being Fortan & Basic when I was a kid and then wrote an extensive Visual Basic program a few years ago which worked well. I'm now trying to tie a computer to several pieces of equipment that will sit there and collect information as it comes in. I managed to put together/find some code that I modified to work, however I found that either 1. It gets one data entry and then stops receiving as the connection gets closed once the data gets sent from one of the outside connections or 2. If left with no data being sent, times out after a while. I do get data from either piece of equipment (2 currently, but more to follow), but once again it only gets the one line of data that is being sent and closed by the sending equipment. Here's the attachments of code and the timeout that comes up if left with no response. Maybe someone can suggest what is required or if I need to work with another format or language to get this working.
Thanks!
import socket, sys
socket.setdefaulttimeout(150)
host = ''
port = 65432
socksize = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: %s" % port)
s.listen(1)
print("Now listening...n")
conn, addr = s.accept()
while True:
print ('New connection from %s:%d' % (addr[0], addr[1]))
data = conn.recv(socksize)
if not data:
break
elif data == 'killsrv':
conn.close()
sys.exit()
else:
print(data)
And if no data sent to it:
Server started on port: 65432
Now listening...n
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python310/TESTSOCKET4.py", line 13, in <module>
conn, addr = s.accept()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\socket.py", line 293, in accept
fd, addr = self._accept()
TimeoutError: timed out
I'm a newbie to Python with my background being Fortan & Basic when I was a kid and then wrote an extensive Visual Basic program a few years ago which worked well. I'm now trying to tie a computer to several pieces of equipment that will sit there and collect information as it comes in. I managed to put together/find some code that I modified to work, however I found that either 1. It gets one data entry and then stops receiving as the connection gets closed once the data gets sent from one of the outside connections or 2. If left with no data being sent, times out after a while. I do get data from either piece of equipment (2 currently, but more to follow), but once again it only gets the one line of data that is being sent and closed by the sending equipment. Here's the attachments of code and the timeout that comes up if left with no response. Maybe someone can suggest what is required or if I need to work with another format or language to get this working.
Thanks!
import socket, sys
socket.setdefaulttimeout(150)
host = ''
port = 65432
socksize = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: %s" % port)
s.listen(1)
print("Now listening...n")
conn, addr = s.accept()
while True:
print ('New connection from %s:%d' % (addr[0], addr[1]))
data = conn.recv(socksize)
if not data:
break
elif data == 'killsrv':
conn.close()
sys.exit()
else:
print(data)
And if no data sent to it:
Server started on port: 65432
Now listening...n
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python310/TESTSOCKET4.py", line 13, in <module>
conn, addr = s.accept()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\socket.py", line 293, in accept
fd, addr = self._accept()
TimeoutError: timed out