Can't connect through socket with Python -
the issue following.
i have following server:
import socket class receiver: tcp_ip = '127.0.0.1' tcp_port = 2999 buffer_size = 20 s = socket.socket(socket.af_inet, socket.sock_stream) s.bind((tcp_ip, tcp_port)) s.listen(1) conn, addr = s.accept() print('connection address:', addr) while 1: data = conn.recv(buffer_size) if not data: break print("received data:", data) conn.send('0x55'.encode()) # echo conn.close()
and client:
import socket import logging class nvgclient: _instance = none def __init__(self): self.s = socket.socket() self.s.settimeout(3) self.connect() return def __del__(self): try: self.s.close() finally: return @staticmethod def getinstance(): if(nvgclient._instance == none): nvgclient._instance = nvgclient() return nvgclient._instance def connect(self): try: print("****** trying_to_connect_to_socket ********") self.s.connect(('127.0.0.0', 2999)) except socket.error: self.s.close() self.s = socket.socket() self.s.settimeout(3) self.connect() logging.error("socket can`t connect! reconnected.") return def send(self, data: bytearray): try: print("****** trying_to_send_data ********") self.s.send(data) logging.info(str(data)) rdata = self.s.recv(1024) if(rdata[0] == 0x55 , rdata[1:5] == data[0:4]): logging.info('nvg ok') return true else: logging.info('nvg bad') except socket.timeout: self.s.close() self.connect() except indexerror: logging.info('server returns nothing. reconnecting.') self.s.close() self.s = socket.socket() self.s.settimeout(3) self.connect() return false
but when try send data, impossible connect server: self.s.connect(('127.0.0.0', 2999)). socket.error.
is there mistakes or wrong in code? other simple examples or telnet, server works well.
you need connect localhost is:
127.0.0.1
and not
127.0.0.0
as wrote client (server okay though)
Comments
Post a Comment