mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-08 06:45:54 +00:00
Modified simple_client
Now it's more descriptive
This commit is contained in:
parent
991abb2402
commit
9ded2c3d22
29
simple_client/client.py
Normal file
29
simple_client/client.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# client.py
|
||||||
|
|
||||||
|
import socket
|
||||||
|
|
||||||
|
HOST, PORT = '127.0.0.1', 1400
|
||||||
|
|
||||||
|
s = socket.socket(
|
||||||
|
|
||||||
|
socket.AF_INET # ADDRESS FAMILIES
|
||||||
|
#Name Purpose
|
||||||
|
#AF_UNIX, AF_LOCAL Local communication
|
||||||
|
#AF_INET IPv4 Internet protocols
|
||||||
|
#AF_INET6 IPv6 Internet protocols
|
||||||
|
#AF_APPLETALK Appletalk
|
||||||
|
#AF_BLUETOOTH Bluetooth
|
||||||
|
|
||||||
|
|
||||||
|
socket.SOCK_STREAM # SOCKET TYPES
|
||||||
|
#Name Way of Interaction
|
||||||
|
#SOCK_STREAM TCP
|
||||||
|
#SOCK_DGRAM UDP
|
||||||
|
)
|
||||||
|
s.connect((HOST, PORT))
|
||||||
|
|
||||||
|
s.send('Hello World'.encode('ascii'))#in UDP use sendto()
|
||||||
|
data = s.recv(1024)#in UDP use recvfrom()
|
||||||
|
|
||||||
|
s.close()#end the connection
|
||||||
|
print(repr(data.decode('ascii')))
|
21
simple_client/server.py
Normal file
21
simple_client/server.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# server.py
|
||||||
|
|
||||||
|
import socket
|
||||||
|
|
||||||
|
HOST, PORT = '127.0.0.1', 1400
|
||||||
|
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#refer to client.py
|
||||||
|
s.bind((HOST, PORT))
|
||||||
|
s.listen(1)#listen for 1 connection
|
||||||
|
|
||||||
|
conn, addr = s.accept()#start the actual data flow
|
||||||
|
|
||||||
|
print('connected to:', addr)
|
||||||
|
|
||||||
|
while 1:
|
||||||
|
data = conn.recv(1024).decode('ascii')#receive 1024 bytes and decode using ascii
|
||||||
|
if not data:
|
||||||
|
break
|
||||||
|
conn.send((data + ' [ addition by server ]').encode('ascii'))
|
||||||
|
|
||||||
|
conn.close()
|
@ -1,14 +0,0 @@
|
|||||||
# client.py
|
|
||||||
|
|
||||||
import socket
|
|
||||||
|
|
||||||
HOST, PORT = '127.0.0.1', 1400
|
|
||||||
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
s.connect((HOST, PORT))
|
|
||||||
|
|
||||||
s.send(b'Hello World')
|
|
||||||
data = s.recv(1024)
|
|
||||||
|
|
||||||
s.close()
|
|
||||||
print(repr(data.decode('ascii')))
|
|
@ -1,21 +0,0 @@
|
|||||||
# server.py
|
|
||||||
|
|
||||||
import socket
|
|
||||||
|
|
||||||
HOST, PORT = '127.0.0.1', 1400
|
|
||||||
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
s.bind((HOST, PORT))
|
|
||||||
s.listen(1)
|
|
||||||
|
|
||||||
conn, addr = s.accept()
|
|
||||||
|
|
||||||
print('connected to:', addr)
|
|
||||||
|
|
||||||
while 1:
|
|
||||||
data = conn.recv(1024)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
conn.send(data + b' [ addition by server ]')
|
|
||||||
|
|
||||||
conn.close()
|
|
Loading…
x
Reference in New Issue
Block a user