Browse Source

..

master
dfstorm 6 months ago
parent
commit
8644675a40
2 changed files with 54 additions and 0 deletions
  1. +31
    -0
      client.py
  2. +23
    -0
      server.py

+ 31
- 0
client.py View File

@ -0,0 +1,31 @@
#!/usr/bin/env python3
import socket
from cmd import Cmd
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = b"Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
class MyPrompt(Cmd):
def do_exit(self, inp):
'''exit the application and close remote server.'''
s.send(b"exit")
data = s.recv(BUFFER_SIZE)
print ("received data:", data)
return True
def do_send(self, inp):
'''Send a message'''
s.send(bytes(inp, "utf8"))
print(inp)
data = s.recv(BUFFER_SIZE)
print ("received data:", data)
MyPrompt().cmdloop()
s.close()

+ 23
- 0
server.py View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
import socket # Import the lib
TCP_IP = '127.0.0.1' # Set the listen address
TCP_PORT = 5005 # set the port
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create the socket
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept() # Accept every connection
print ('Connection address:', addr) # display connection detail.
while 1:
data = conn.recv(BUFFER_SIZE)
print ("received data:", data) # Print what you recive.
conn.send(data) # echo
if (data == b"exit"): # Exit the program once you recieve "exit".
break
conn.close() #Close the connection.

Loading…
Cancel
Save