I've written a small tcp server in python 2.7. The code is below. It was deployed on Centos 7:
- At start I got the error "Errno: 104 - Peer closed the socket connection." in wireshark so I've opened the destination port
- open the port: /etc/sysconfig/iptables added rule: "-A INPUT -m state --state NEW -p tcp --dport 5555 -j ACCEPT" than restarted using "sudo systemctl restart iptables"
- I tried rebooting etc' still get errors
In wireshark, after opening the port I get error:
my python script based on this answer and this post and I see nothing in the log.. like nothing is finishing even the connection.
def startServer(self,host,port):
s = socket.socket()
try:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
except SocketError as e:
print ("TS: SocketError connection error %s" % e)
sys.exit()
# Tell the server socket file descriptor to destroy itself when this program ends.
socketFlags = fcntl.fcntl(s.fileno(), fcntl.F_GETFD)
socketFlags |= fcntl.FD_CLOEXEC
fcntl.fcntl(s.fileno(), fcntl.F_SETFD, socketFlags)
s.listen(128) # Allow a queue of up to 128 requests (connections).
s.setblocking(0) # Use asynchronous sockets.
fdmap = {s.fileno():s}
# Listen to socket events on the server socket defined by the above bind() call.
epoll = select.epoll()
epoll.register(s.fileno(), select.EPOLLIN)
connections = {}
requests = {}
responses = {}
address = {}
frameinfo = getframeinfo(currentframe()) #PT_INFO: for debug only..
while True:
try:
events = epoll.poll(10) #TODO: improve this ...
except: #TODO: improve this ...
pass
for fd,event in events:
if fd is s.fileno():
# connection is a new socket object.
# address is client IP address. The format of address depends on the address family of the socket (i.e., AF_INET).
connection, address = s.accept()
print "got connnection req from: %s" % str(address)
# Set new socket-connection to non-blocking mode.
connection.setblocking(0)
# Listen for read events on the new socket-connection.
epoll.register(connection.fileno(), select.EPOLLIN)
connections[connection.fileno()] = connection
requests[connection.fileno()] = b''
else:
if event & select.EPOLLIN:
try:
# requests[fd] = connections[fd].recv(1024)
while(1):
t_data = connections[fd].recv(1024)
logLine = ("TS: got data %s" % t_data)
data += t_data
except SocketError as e:
if ex.errno == 11: # EAGAIN
# Nothing more to read;
pass
continue
#do somthing with the data
print "got data:" + data
# A socket was closed on our end.
elif event & select.EPOLLHUP:
print "Closed connection to", connections[fd]['address']
epoll.unregister(fd)
del connections[fd]
# Error on a connection.
elif event & select.EPOLLERR:
print "Error on connection to", connections[fd]['address']
epoll.modify(fd, 0)
connections[fd]['connection'].shutdown(socket.SHUT_RDWR)
else:
#test connnection is still no by writing to the socket
try:
# todo = 'write to the socket so it will give error'
print "sending ' ' to socket to test if it is still up"
connection.send(" ")
except:
print "after sending ' ' to socket got exception"
epoll.modify(fd, 0)
connections[fd]['connection'].shutdown(socket.SHUT_RDWR)