Catch Socket Error Python
Contents |
here for a quick overview of the site Help Center Detailed answers to
Python Socket.timeout Exception
any questions you might have Meta Discuss the workings and python try except socket error policies of this site About Us Learn more about Stack Overflow the company Business Learn more
Python Socket Error Permission Denied
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community python socket error 10054 Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python socket connection exception up vote 4 down vote favorite 1 I have a socket-connection going on and I wanna improve the exception handling and Im stuck. python socket error connection reset by peer Whenever I use the socket.connect(server_address) function with an invalid argument the program stops, but doesnt seem to throw any exceptions. Heres my code import socket import sys import struct class ARToolkit(): def __init__(self): self.x = 0 self.y = 0 self.z = 0 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.logging = False def connect(self,server_address): try: self.sock.connect(server_address) except socket.error, msg: print "Couldnt connect with the socket-server: %s\n terminating program" % msg sys.exit(1) def initiate(self): self.sock.send("start_logging") def log(self): self.logging = True buf = self.sock.recv(6000) if len(buf)>0: nbuf = buf[len(buf)-12:len(buf)] self.x, self.y, self.z = struct.unpack(" Server with Select A Long-Lived Echo Client The Threading Module Code Index License This work is licensed under a Creative Commons License. The Socket Module Catching Exceptions In the previous examples, we have not checked for any exceptions that could be raised by methods in the socket module. One clear python socket error broken pipe example is when the server tries to bind its socket to a particular port. Only one process is allowed to bind to each port; if this port is already being used then an exception is raised. Here is the modified echo server that catches some of these exceptions (echoserver-simple-exceptions.py): #!/usr/bin/env python """ A http://stackoverflow.com/questions/25447803/python-socket-connection-exception simple echo server that handles exceptions """ import socket host = '' port = 50000 backlog = 5 size = 1024 s = None try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host,port)) s.listen(backlog) except socket.error, (value,message): if s: s.close() print "Could not open socket: " + message sys.exit(1) while 1: client, address = s.accept() data = client.recv(size) if data: client.send(data) client.close() The part that has changed is where the http://ilab.cs.byu.edu/python/socket/exceptions.html socket is initialized: s = None try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host,port)) s.listen(backlog) except socket.error, (value,message): if s: s.close() print "Could not open socket: " + message sys.exit(1) This code is surrounded by a try-except statement, and the except portion contains two values: the socket.error exception, and its return value. The socket.error exception is raised for socket-related errors; the socket module defines several other exceptions for address-related errors. The return value of the socket is a tuple consisting of an error code and message. Try running this server twice, and the second time you should be able to see the exception that is caught. Run the original server twice if you want to see what happens when the exception is not caught. As another example, here is the echo client modified to catch some exceptions (echoclient-simple-exceptions.py): #!/usr/bin/env python """ A simple echo client that handles some exceptions """ import socket import sys host = 'localhost' port = 50000 size = 1024 s = None try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host,port)) except socket.error, (value,message): if s: s.close() print "Could not open socket: " + message sys.exit(1) s.send('Hello, world') data = s.recv(size) s.close() print 'Received:', data Try running this ver Error HandleError Handling Example import socket, sys host = sys.argv[1] textport = sys.argv[2] http://www.java2s.com/Code/Python/Network/ErrorHandlingExample.htm filename = sys.argv[3] try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, e: print https://bytes.com/topic/python/answers/448115-catching-very-specific-exceptions "Strange error creating socket: %s" % e sys.exit(1) try: port = int(textport) except socket error ValueError: print "Couldn't find your port: %s" % e sys.exit(1) try: s.connect((host, port)) except socket.gaierror, e: print "Address-related error connecting to server: %s" % e python socket error sys.exit(1) except socket.error, e: print "Connection error: %s" % e sys.exit(1) try: s.sendall("GET %s HTTP/1.0\r\n\r\n" % filename) except socket.error, e: print "Error sending data: %s" % e sys.exit(1) while 1: try: buf = s.recv(2048) except socket.error, e: print "Error receiving data: %s" % e sys.exit(1) if not len(buf): break sys.stdout.write(buf) Related examples in the same category1.Create handler for socket handler2.Server With Error Handlingjava2s.com |Email:info at java2s.com|© Demo Source and Support. All rights reserved. question and get tips & solutions from a community of 418,490 IT Pros & Developers. It's quick & easy. Catching very specific exceptions P: n/a Harlin Seritt I am running the following code: import socket host = '9.9.45.103' port = 10001 conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) conn.connect((host, port)) When conn.connect() is run, there can be two different exceptions: socket.error: (10061, 'Connection refused') socket.error: (10060, 'Operation timed out') How can I set up exceptions for each of these differently? Normally I would use: except socket.error: code goes here... Of course, though, these are two separate error messages under the same error handler though. I've tried: except socket.error, (10061, 'Connection refused'): and except (socket.error, 10061, 'Connection refused'): to no avail. What do I need to do to catch each one specifically? Thanks, Harlin Seritt Jan 22 '06 #1 Post Reply Share this Question 6 Replies P: n/a Alex Martelli Harlin Seritt Python Socket Error Bad File Descriptor
Python Socket Error 99