Skip to content

Commit ca188ee

Browse files
committed
Improved Error Handling
Not only did I improve error handling I also removed unneeded pieces of code.
1 parent 7ca8bef commit ca188ee

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

interface.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
## Interface ##
2-
## v 0.9 ##
2+
## v 0.92 ##
33

44
import tcp
55

6-
def inter(remote_ip, port, data=""):
6+
## Will return False if there is an error
7+
def inter(remote_ip, port, data):
78
rep = 0
8-
#Can be IP or domain name
9-
#remote_ip = input("Host address: ")
10-
11-
#The port that you want to connect to
12-
#port = int(input("Input Port: "))
13-
14-
#data = input("Enter data: ")
159
if data != "":
1610
s = tcp.connect(remote_ip, port)
1711

@@ -36,7 +30,8 @@ def inter(remote_ip, port, data=""):
3630
else:
3731
return False
3832

39-
ip = '192.168.1.11'
40-
port = 5050
41-
info = inter(ip, port,data=input("Enter Data: "))
42-
print(info)
33+
if __name__ == "__main__":
34+
ip = input("Enter ip to connect to: ")
35+
port = 5050
36+
info = inter(ip, port,data=input("Enter Data: "))
37+
print(info)

server.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
## Example use of tcp v0.92 ##
1+
## Example use of tcp v0.93 ##
22
## R. M. Jones ##
33
## 01/07/2017 ##
44

55
import tcp #imoprts the module that is included with this file
66

77
def server(BUFFER_SIZE, TCP_IP=tcp.getmyip(), TCP_PORT=5050):
8-
9-
data =0
10-
#TCP_IP = tcp.getmyip() #Gets your private ip address
11-
#TCP_PORT = int(input("Enter port: ")) #The port that will be open
12-
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
13-
148
conn = tcp.bind(TCP_IP, TCP_PORT)
15-
16-
9+
1710
data = tcp.receive(conn, BUFFER_SIZE)
1811
if not data:
1912
conn = tcp.bind(TCP_IP, TCP_PORT)
2013
else:
2114
return "received data: " + data, conn
22-
15+
2316

2417
if __name__ == "__main__":
2518
info = 0
19+
s = 0
2620
while info != "stop":
27-
info, s = server(20)
28-
print(info)
29-
tcp.send(info, s)
30-
s.close()
21+
try:
22+
info, s = server(20)
23+
print(info)
24+
tcp.send(info, s)
25+
s.close()
26+
except TypeError:
27+
print("Error, Type Error")
28+
tcp.send("Repeat", s)

tcp.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,22 @@ def getmyip():
3737

3838
#The default ip is your local one and port is 5050
3939
def bind(TCP_IP=getmyip(), TCP_PORT=5050):
40-
s = create()
41-
s.bind((TCP_IP, TCP_PORT))
42-
print("Ready to connect on: "+TCP_IP+" on port:", TCP_PORT)
40+
try:
41+
s = create()
42+
s.bind((TCP_IP, TCP_PORT))
43+
print("Ready to connect on: "+TCP_IP+" on port:", TCP_PORT)
44+
except OSError:
45+
print("Cannot connect please check your PC")
46+
exit()
47+
48+
except:
49+
print("Ready to connect on: "+TCP_IP+" on port:", TCP_PORT)
50+
4351
s.listen(1)
4452
conn, addr = s.accept()
4553
print ('Connection address:', addr)
4654
return conn
55+
4756

4857
def create():
4958
try:
@@ -75,6 +84,10 @@ def connect(remote_ip, port):
7584
except ConnectionRefusedError:
7685
print("Connection Refused Error (likely because other machine's port isn't open)")
7786
return False
87+
88+
except TimeoutError:
89+
print("Connection timed out")
90+
return False
7891

7992
except ConnectionRefusedError:
8093
print("Connection Refused Error (likely because other machine's port isn't open)")
@@ -105,4 +118,4 @@ def receive(s, buff=4069):
105118
except ConnectionAbortedError:
106119
print("Error ConnectionAbortedError")
107120
return False
108-
#connect(socket.gethostbyname(socket.gethostname()), 5005)
121+

0 commit comments

Comments
 (0)