Skip to content

Commit 5323af1

Browse files
committed
First Release (Alpha)
Updated the README file and added the python programs into the project. This is the initial release for this project.
1 parent 5b7d810 commit 5323af1

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# python_tcp
22
Python TCP (can be used to communicate between devices)
3+
4+
This is my python networking project. The end goal is to be able to communicate easily between two devices, where the connecting is easy etc.
5+
6+
The software will be a module but I will be creating an example of how it can be used.
7+
8+
At the moment there will be two main components to the project. The client part and the server part. At the end both should work perfectly together.
9+
10+
This project is a work in progress and is still in the early alpha stage.
11+
12+
You're welcome to have a look at the code and recommend changes or features you would like to be added.
13+
14+
This will be my first project on git and I hope you'll enjoy the journey from alpha to beta to full release.
15+
16+
Have a great day :)
17+
RhysMJ
18+
19+
PS - I have no instructions yet as I have no idea what will change in the program. I'm planning in creating instuctions when beta is released and by then the code will be commented on to make it easier to understand.

client.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## TCP Second Try ##
2+
## Component Connect to other Machine ##
3+
4+
import socket
5+
import sys
6+
import re
7+
def validate(Type, data):
8+
ip = re.compile("^[0-9]{1,3}[.]{1}[0-9]{1,3}[.]{1}[0-9]{1,3}[.]{1}$")
9+
10+
if Type == 'ip':
11+
if ip.match(data):
12+
return True
13+
else:
14+
return False
15+
16+
else:
17+
return "No Type: "+Type
18+
19+
#if domain name is sent then it is resolved here
20+
def domain(host):
21+
try:
22+
remote_ip = socket.gethostbyname(host)
23+
return remote_ip
24+
25+
except socket.gaierror:
26+
print("Host name could not be resolved")
27+
return False
28+
def create():
29+
try:
30+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
31+
return s
32+
except socket.error:
33+
print("Failed to connect")
34+
return False
35+
36+
def connect(remote_ip, port):
37+
38+
39+
if not validate('ip', remote_ip):
40+
remote_ip = domain(remote_ip)
41+
42+
print("Connecting to: " + remote_ip+" Port:",port)
43+
try:
44+
s.connect((remote_ip, port))
45+
46+
47+
except NameError:
48+
try:
49+
s = create()
50+
s.connect((remote_ip, port))
51+
52+
except ConnectionRefusedError:
53+
print("Connection Refused Error (likely because other machine's port isn't open)")
54+
return False
55+
56+
except ConnectionRefusedError:
57+
print("Connection Refused Error (likely because other machine's port isn't open)")
58+
return False
59+
60+
except:
61+
print("Unknown Error")
62+
return False
63+
print("Socket connected to IP: " + remote_ip)
64+
return True
65+
connect("127.0.0.1", 80)

server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import socket
2+
data =0
3+
TCP_IP = '192.168.1.8'
4+
TCP_PORT = 5005
5+
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
6+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7+
s.bind((TCP_IP, TCP_PORT))
8+
s.listen(1)
9+
10+
conn, addr = s.accept()
11+
print ('Connection address:', addr)
12+
while data != "stop":
13+
data = conn.recv(BUFFER_SIZE).decode()
14+
if not data: break
15+
print ("received data:", data)
16+
conn.send(data.encode()) # echo
17+
conn.close()

0 commit comments

Comments
 (0)