dfstorm 4 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 @@
1
+#!/usr/bin/env python3
2
+
3
+import socket
4
+from cmd import Cmd
5
+
6
+TCP_IP = '127.0.0.1'
7
+TCP_PORT = 5005
8
+BUFFER_SIZE = 1024
9
+MESSAGE = b"Hello, World!"
10
+
11
+s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
12
+s.connect((TCP_IP, TCP_PORT))
13
+
14
+class MyPrompt(Cmd):
15
+   def do_exit(self, inp):
16
+        '''exit the application and close remote server.'''
17
+        s.send(b"exit")
18
+        data = s.recv(BUFFER_SIZE)
19
+        print ("received data:", data)
20
+        return True
21
+   def do_send(self, inp):
22
+        '''Send a message'''
23
+        s.send(bytes(inp, "utf8"))
24
+        print(inp)
25
+        data = s.recv(BUFFER_SIZE)
26
+        print ("received data:", data)
27
+
28
+MyPrompt().cmdloop()
29
+s.close()
30
+
31
+

+ 23
- 0
server.py View File

@@ -0,0 +1,23 @@
1
+#!/usr/bin/env python3
2
+
3
+import socket # Import the lib
4
+
5
+TCP_IP = '127.0.0.1' # Set the listen address
6
+TCP_PORT = 5005 # set the port
7
+BUFFER_SIZE = 20  # Normally 1024, but we want fast response
8
+
9
+s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create the socket
10
+s.bind((TCP_IP, TCP_PORT))
11
+s.listen(1)
12
+
13
+conn, addr = s.accept() # Accept every connection
14
+
15
+print ('Connection address:', addr) # display connection detail.
16
+
17
+while 1:
18
+    data = conn.recv(BUFFER_SIZE)
19
+    print ("received data:", data) # Print what you recive.
20
+    conn.send(data)  # echo
21
+    if (data == b"exit"): # Exit the program once you recieve "exit".
22
+        break
23
+conn.close() #Close the connection.

Loading…
Cancel
Save