Errno 107 Transport endpoint is not connected with Socket - python

I'm trying to make a connection tester with python and made this function:
import socket as skt
import sys
def main():
try:
s = skt.socket(skt.AF_INET, skt.SOCK_STREAM, 0)
except skt.error as e:
print('A conexão falhou! :(')
print(f'Erro {e}.')
sys.exit()
print("Socket criado com sucesso!")
HostTarget = input('Digite o Host ou IP a ser conectado: ')
DoorTarget = (input('Digite a porta a ser conetcada: '))
try:
s.connect:((HostTarget, int(DoorTarget)))
print(f'Cliente conectado com sucesso no Host: {HostTarget} através da porta: {DoorTarget}.')
s.shutdown(2)
except skt.error as e:
print(f'Não foi possível se conectar ao Host: {HostTarget} através da porta: {DoorTarget}.')
print(f'Erro: {e}')
sys.exit()
if __name__ == "__main__":
main()
But when I run the test, I get the following error:
Erro: [Errno 107] Transport endpoint is not connected
An exception has occurred, use %tb to see the full traceback.

Related

Socket Problem in Python when send data to server

I have a problem when I send data from client to server in python INET Stream socket.
I send a dictionary using library json of python , and , in the server side, I create a Ticket object and save in my MySQL DB.
At this point, the program don't have any problems, but, when i try save other ticket in the same ejecution of client, the server not recieve the data and , the ticket don't save in the DB. Any solutions?
In server.py
def thread_fuction(port,sock):
while True:
msg = clientsocket.recv(1024)
print(f"Recibido del puerto {port} atendido por PID {os.getpid()}: {msg.decode()}")
logger(sock,msg)
if (msg.decode() == 'INSERTAR'):
dict_data=sock.recv(1024).decode()
final_data=json.loads(dict_data)
final_data=dict(final_data)
for key,value in final_data.items():
if key == "autor":
autor=value
elif key == "titulo":
titulo=value
elif key == "descripcion":
descripcion=value
elif key == "estado":
estado=value
print(final_data)
ticket=Ticket(autor=autor,titulo=titulo,descripcion=descripcion,estado=estado,fecha=datetime.now())
session.add(ticket)
session.commit()
break
# creamos el objeto socket
try:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print('Fallo al crear el socket!')
sys.exit()
#Establecemos parametros
host = "localhost"
port = int(8070)
# Blindeamos el puerto y el host
serversocket.bind((host, port))
# Establecemos 5 peticiones de escucha como maximo.
serversocket.listen(5)
if __name__ == "__main__":
while True:
# establish a connection
clientsocket, addr = serversocket.accept()
print('Conexion establecida: SERVER ON')
conection=Thread(target=thread_fuction,args=(port,clientsocket))
conection.start()
In client.py
try:
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket Creado!")
except socket.error:
print ('Fallo al crear el socket!')
sys.exit()
host = "localhost"
port = int(8070)
client_socket.connect((host, port))
print ('Socket conectado al host', host, 'en el puerto', port)
while True:
print("""\n
\t\t\t *** Menu ***
- INSERTAR TICKET (INSERTAR)
- SALIR (SALIR)
""")
opcion = input('Opcion: ').upper()
client_socket.sendto(opcion.encode(), (host, port))
if (opcion == 'INSERTAR'):
autor = input("\nIngrese autor del Ticket: ")
titulo = input("\nIngrese titulo del ticket: ")
descripcion = input("\nIngrese descripcion del ticket: ")
estado = input("\nIngrese estado del ticket (pendiente, en procesamiento o resuelto): ")
while validar_estado(estado):
estado=input("Estado debe ser uno de los pedidos, intentelo nuevamente): ")
data={"autor":autor,"titulo":titulo,"descripcion":descripcion,"estado":estado}
json_data=json.dumps(data) #Convertimos el diccionario a JSON
client_socket.sendto(json_data.encode(),(host,port))

I am not able to stream images from a picamera

I found some code here for a project at : https://picamera.readthedocs.io/en/release-1.13/recipes2.html#rapid-capture-and-streaming section 4.9
I successfully make it work but when i tried to put the serveur on the raspberry py instead of the client, i would not work.
EDIT:
EDIT:
I found the answer : On the server the file needs to be open in write (wb) and on the client it needs to be open in read (rb)
However, we get 7 secondes of latency beteen the server and the client, Do you know how i could lower it ?
LE SERVEUR
# -*-coding:utf-8 -*
import io
import socket
import struct
from PIL import Image, ImageTk
from threading import Thread
import time
class ControleurClientVideo():
def __init__(self, controleur_client):
self.adresse='0.0.0.0'
self.port=8000
self._client_socket = socket.socket()
self._connection = None
self._thread = None
self._stop = False
def connection_raspberry(self):
self._thread = Thread(target=self._connection_avec_raspberry)
self._thread.start()
def _connection_avec_raspberry(self):
try:
self._client_socket.connect((self.adresse, self.port))
self._connection = self._client_socket.makefile('wb')
self._connection_active=True
print("Connection avec le serveur etabli")
time.sleep(2)
self._recevoir_flux_image()
except Exception as e:
print(e)
def _recevoir_flux_image(self):
try:
while not (self._stop):
# Read the length of the image as a 32-bit unsigned int. If the
# length is zero, quit the loop
image_len = struct.unpack('<L',
self._connection.read(struct.calcsize('<L')))[0]
if not image_len:
self.connection_perdu = True
break
# Construct a stream to hold the image data and read the image
# data from the connection
image_stream = io.BytesIO()
image_stream.write(self._connection.read(image_len))
# Rewind the stream, open it as an image with PIL and do some
image_stream.seek(0)
image_pill = Image.open(image_stream)
image_pill = image_pill.resize((320, 240), Image.ANTIALIAS)
image_tk = ImageTk.PhotoImage(image_pill)
print(image_tk)
self.controleur_client.changer_image(image_tk)
finally:
self.fermer_connection()
def fermer_connection(self):
self._stop = True
time.sleep(0.5)
if not (self._connection == None):
self._connection.close()
self._connection = None
self._client_socket.close()
self._client_socket=None
self._thread = None
print("Connection avec le serveur fermer")
LE CLIENT
# -*-coding:utf-8 -*
import io
import socket
import struct
import time
import picamera
from threading import Thread
class ControleurStreamingVideo():
def __init__(self):
self.adresse='0.0.0.0'
self.port=8000
self._serveur_socket = socket.socket()
self._serveur_socket.bind((self.adresse, self.port))
self._connection = None
self._thread=None
def ouvrir_serveur(self):
self._thread = Thread(target=self._connection_avec_client)
self._thread.start()
def _connection_avec_client(self):
try:
print("Serveur en attente d'une connection...")
self._serveur_socket.listen(5)
self._connection = self._serveur_socket.accept()[0].makefile('rb')
print("Connection réussi, début de la vidéo")
except Exception as e:
repr(e)
finally:
self._envoit_image()
self._serveur_socket.close()
def _envoit_image(self):
try:
self.output = SplitFrames(self._connection)
with picamera.PiCamera(resolution='VGA', framerate=30) as camera:
time.sleep(1) #warmup la caméra
camera.start_recording(self.output, format='mjpeg')
camera.wait_recording(30)
camera.stop_recording()
self._serveur_socket.close()
except Exception as e:
print(e)
class SplitFrames(object):
def __init__(self, connection):
self.connection = connection
self.stream = io.BytesIO()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# Start of new frame; send the old one's length
# then the data
size = self.stream.tell()
if size > 0:
self.connection.write(struct.pack('<L', size))
self.connection.flush()
self.stream.seek(0)
self.connection.write(self.stream.read(size))
self.stream.seek(0)
self.stream.write(buf)
When i run the program it says only write for error. I found out that the problem come from the method write in SplitFrames. Any idea on what is causing this
SERVEUR:
Serveur en attente d'une connection...
Connection réussi, début de la vidéo
write
CLIENT:
Connection avec le serveur etabli
Connection avec le serveur fermer
read
EDIT:
I found the answer : On the server the file needs to be open in write (wb) and on the client it needs to be open in read (rb)
However, we get 7 secondes of latency beteen the server and the client, Do you know how i could lower it ?

Python Asyncore executes handle_read when client disconnect

I was a socket TCP server in python using asyncore. My handle_read function is:
def handle_read(self):
data = self.recv(50)
'''interpretar os comandos:
operação: Ligar/Desligar Bomba, Ligar/Desligar Aquecedor, Alterar velocidade da bomba
Modo: trocar de modo automático para remoto
Armazenamento: ativar ou desativar o armazenamento de dados para o trend e
também apagar dados
'''
if len(data) < 2: #comandos digitais
try:
process_commands(data)
except Exception as err:
print(str(err))
else: #comando analogico
try:
ld = json.loads(data.decode('utf-8'))
bytescommand = pack('f',ld['pump_speed'])
bus.write_block_data(arduinoAddress,53,list(bytescommand))
except Exception as err:
print(str(err))
finally:
pass
Look, i test the data received to execute functions. But when a client disconnect, the program returns:
"char format requires a bytes object of lenght 1"
This indicates that handle_read function executes when client disconnect.
It's normal? How i proccess the situation?
Thanks a lot!
That is a normal call. When the peer closes its socket (or simply shutdowns it with SH_WR) a read call returns immediately with 0 bytes.
So your handle_read should be prepared for that end of file on the socket:
def handle_read(self):
...
if len(data) == 0: # EOF on input socket
pass # or add disconnection processing...
elif len(data) < 2: #comandos digitais
...

How to keep trying to establish connection in Python

If the server is not up when I try to run the following code, I just get a Connection refused error.
How can I make the sender below to keep trying to establish connection and perhaps sending until the remote server is indeed up and the connection is successfully established?
HOST = client_ip # The remote host
PORT = port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(msg)
if expect_receive:
received_data = s.recv(1024)
print received_data
#client has started
s.close()
return
How about brute force? Something like this
import time
while 1:
HOST = client_ip # The remote host
PORT = port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((HOST, PORT))
except:
print("FAILED. Sleep briefly & try again")
time.sleep(10)
continue
s.sendall(msg)
if expect_receive:
received_data = s.recv(1024)
print received_data
#client has started
s.close()
return
I am using ssl with threads and the following works for me.
import socket, ssl
from threading import *
from _thread import *
from time import sleep
HOST_1, PORT_1, CERT_1 = '127.0.0.1', 443, 'certificate_1.pem'
HOST_2, PORT_2 = '127.0.0.1', 4430
def enviar():
#global morte;
sock = socket.socket(socket.AF_INET)
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH);
context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1
context.check_hostname = False;
context.load_verify_locations(cafile='certificate_2.pem');
conn = context.wrap_socket(sock, server_hostname=HOST_2);
while True:
try:
conn.connect((HOST_2, PORT_2));
break;
except:
print("\n=====Failed connection=====\n");
sleep(1);#vital, without it the script raises an exception
try:
while True:
x = input("--> ");
x = bytes(x, "utf-8");
conn.write(x)
print(conn.recv().decode())
except ssl.SSLError as e:
print(e);
except Exception as e:
print(e);
except KeyboardInterrupt:
print("\n====Bye!====\n");
if conn:
conn.close();
#morte = True;
###################
#parte original abaixo
def receber():
sock = socket.socket();
sock.bind((HOST_1, PORT_1));
sock.listen(5);
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH);
context.load_cert_chain(certfile=CERT_1, keyfile="private_1.pem") # 1. key, 2. cert, 3. intermediates
context.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 # optional
context.set_ciphers('EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH')
while True:
conn = None;
ssock, addr = sock.accept();
try:
conn = context.wrap_socket(ssock, server_side=True);
while True:
print(conn.recv());
conn.write(b'HTTP/1.1 200 OK\n\n%s' % conn.getpeername()[0].encode());
break
except ssl.SSLError as e:
print(e)
except Exception as e:
print(e);
if conn:
conn.close();
finally:
if conn:
conn.close()
print("End!");
if __name__ == '__main__':
start_new_thread(receber, ());#1
start_new_thread(enviar, ());#2
If you run in association with another similar script (using other private key and certificates, obviously) it will run mostly ok, but, it will raise an:
EOF occurred in violation of protocol (_ssl.c:2472)
I am still trying to figure out how to deal with it.

Connection reset by peer [errno 104] in Python 2.7

I've seen and read a lot about this particular issue on the internet.
I am writing a simple chat server and client using socket in python for learning purpose mainly.
I've observed an issue here.
Here is my server code :
__author__ = 'pchakraverti'
import socket
import select
import sys
class NickSocketMap(object):
count = 0
def __init__(self, nick, client_socket):
self.nick = nick
self.client_socket = client_socket
NickSocketMap.count += 1
#staticmethod
def display_count():
print "Total number of clients is %d" % NickSocketMap.count
host = ""
port = 7575
socket_list = []
nick_list = []
cnt = 0
recv_buffer = 1024
def register_nick(nick, client_socket):
obj = NickSocketMap(nick, client_socket)
nick_list.append(obj)
def process_request(request_string, client_socket):
parts = request_string.split("|")
if parts[0] == "set_nick":
register_nick(parts[1], client_socket)
client_socket.send("nick_set")
elif parts[0] == "transmit_msg":
broadcast_message(parts[1], parts[2])
return 1
def broadcast_message(message, client_nick):
for s in nick_list:
if s.nick == client_nick:
try:
s.client_socket.send(message)
except socket.errno, ex:
print ex
break
def run_server():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind((host, port))
except socket.errno, ex:
print ex
sys.exit(-1)
sock.listen(10)
# add the parent socket in the list
socket_list.append(sock)
# keep the server alive
while True:
try:
read_ready, write_ready, in_error = select.select(socket_list, [], [], 0)
except select.error, ex:
print ex
continue
for s in read_ready:
# check if s is the parent socket
if s == sock:
# accept new connection and append to list
try:
con, addr = s.accept()
if con not in socket_list:
socket_list.append(con)
except socket.errno, ex:
print ex
else:
try:
# receive packet from connected client
packet = s.recv(recv_buffer)
if not packet:
socket_list.remove(s)
read_ready.remove(s)
for n in nick_list:
if n.client_socket == s:
nick_list.remove(n)
break
break
print packet
except socket.errno, ex:
print ex
continue
process_request(packet, s)
sock.close()
if __name__ == "__main__":
run_server()
and here is my client code:
__author__ = 'pchakraverti'
import socket
nick = ""
host = "192.168.0.167"
port = 7575
sock = ""
def welcome():
print "Welecome to SecuChat!"
print "---------------------"
def init():
nick = raw_input("Enter your chat nickname : ")
print nick
global sock
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((host, port))
except socket.errno, ex:
print ex
sock.send("set_nick|"+nick)
#sock.close()
if __name__ == "__main__":
welcome()
init()
In the client code, when I don't do the sock.close(), the server runs into an exception :
Traceback (most recent call last):
File "server.py", line 102, in <module>
run_server()
File "server.py", line 84, in run_server
packet = s.recv(recv_buffer)
socket.error: [Errno 104] Connection reset by peer
how ever, when I add that line, the problem doesn't occur.
Now I've two questions :
i) I've handled exceptions in the server.py, why is this exception not being handled and why is it crashing the code ? How can I make the server more robust and what am I missing ?
ii) What is the logic behind this crash and exception in relation to the sock.close() line in the client ?
i) Your try-except block doesn't catch any exceptions.
The first argument to except must be the type of the exception you want to catch. socket.errno is not an exception class but a module. You need to catch socket.error:
except socket.error, ex:
print ex
It "crashes" your code because any exception that isn't handled somewhere in the call stack propagates outwards until it hits an except. If there is no handler the program is terminated.
ii) When the client terminates without closing the connection, a RST packet is sent by the TCP/IP stack of your OS. This is roughly the equivalent of hanging up a phone without saying goodbye. Python converts this into an exception with the text "Connection reset by peer". It simply means that since you called read() Python assumed you expect to receive something and when the connection suddenly disconnected, Python informs you of this by raising the exception.

Categories

Resources