Skip to content

Commit 0bc9c35

Browse files
committed
TCP Module v0.9
This is the TCP module which can be used with or without the example program. This module makes it easier to setup TCP and uses the socket module which is built into python.
1 parent 5323af1 commit 0bc9c35

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

tcp.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
## TCP Easy Setup Module v0.9##
2+
## Component Connect to other Machine ##
3+
## R. M. Jones ##
4+
5+
import socket
6+
import sys
7+
import re
8+
9+
def validate(Type, data):
10+
ip = re.compile("^[0-9]{1,3}[.]{1}[0-9]{1,3}[.]{1}[0-9]{1,3}[.]{1}$")
11+
12+
if Type == 'ip':
13+
if ip.match(data):
14+
return True
15+
else:
16+
return False
17+
18+
else:
19+
return "No Type: "+Type
20+
21+
#if domain name is sent then it is resolved here
22+
def domain(host):
23+
try:
24+
remote_ip = socket.gethostbyname(host)
25+
return remote_ip
26+
27+
except socket.gaierror:
28+
print("Host name could not be resolved")
29+
return False
30+
31+
def getmyip():
32+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
33+
s.connect(("1.1.1.1", 80))
34+
TCP_IP = s.getsockname()[0]
35+
s.close()
36+
return TCP_IP
37+
38+
#The default ip is your local one and port is 5050
39+
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)
43+
s.listen(1)
44+
conn, addr = s.accept()
45+
print ('Connection address:', addr)
46+
return conn
47+
48+
def create():
49+
try:
50+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
51+
return s
52+
except socket.error:
53+
print("Failed to connect")
54+
return False
55+
56+
57+
def connect(remote_ip, port):
58+
59+
if not validate('ip', remote_ip):
60+
remote_ip = domain(remote_ip)
61+
if remote_ip == False:
62+
print("Error Resolving Host name")
63+
return False
64+
65+
print("Connecting to: " + remote_ip+" Port:",port)
66+
try:
67+
s.connect((remote_ip, port))
68+
69+
70+
except NameError:
71+
try:
72+
s = create()
73+
s.connect((remote_ip, port))
74+
75+
except ConnectionRefusedError:
76+
print("Connection Refused Error (likely because other machine's port isn't open)")
77+
return False
78+
79+
except ConnectionRefusedError:
80+
print("Connection Refused Error (likely because other machine's port isn't open)")
81+
return False
82+
83+
except:
84+
print("Unknown Error")
85+
return False
86+
print("Socket connected to IP: " + remote_ip)
87+
return s
88+
89+
def send(data, s):
90+
try:
91+
s.sendall(data.encode())
92+
return True
93+
except:
94+
print("Couldn't send data")
95+
return False
96+
97+
def receive(s, buff=4069):
98+
try:
99+
return s.recv(buff).decode()
100+
101+
except ConnectionResetError:
102+
print("Error ConnectionResetError")
103+
return False
104+
105+
except ConnectionAbortedError:
106+
print("Error ConnectionAbortedError")
107+
return False
108+
#connect(socket.gethostbyname(socket.gethostname()), 5005)

0 commit comments

Comments
 (0)