Python Forum
TCP Communicate... - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: TCP Communicate... (/thread-31440.html)



TCP Communicate... - ATARI_LIVE - Dec-11-2020

I want to test to see if works, here what I am trying and did not work, why? Byte?

Here for CLIENT:
import socket


TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('HELLO WORLD')
data = s.recv(BUFFER_SIZE)
s.close()

print ("received data:", data)
Here for SERVER:
import socket


TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 20  # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()
print ('Connection address:', addr)
while 1:
    data = conn.recv(BUFFER_SIZE)
    if not data: break
    print ("received data:", data)
    conn.send(data)  # echo
conn.close()
On CLIENT message said:
Output:
Traceback (most recent call last): File "client.py", line 11, in <module> s.send('HELLO WORLD') TypeError: a bytes-like object is required, not 'str'
THANKS!


RE: TCP Communicate... - ATARI_LIVE - Dec-11-2020

I think I fixed, line 11 on client: s.send(bytes('HELLO WORLD', 'UTF-8')) got it worked.


RE: TCP Communicate... - DeaD_EyE - Dec-11-2020

import socket
from argparse import ArgumentParser

TCP_IP = "127.0.0.1"
TCP_PORT = 5005
BUFFER_SIZE = 1024
SMALL_BUFFER_SIZE = 20
MESSAGE = "HELLO WORLD"


def client():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.connect((TCP_IP, TCP_PORT))
        sock.send(MESSAGE.encode())
        data = sock.recv(BUFFER_SIZE)


def server():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind((TCP_IP, TCP_PORT))
        sock.listen(1)
        conn, (client_ip, client_port) = sock.accept()
        print("Connection address:", client_ip)
        print("Connection port:", client_port)
        while True:
            data = conn.recv(SMALL_BUFFER_SIZE)
            if not data:
                break
            print("received data:", data.decode(errors="ignore"))
            conn.send(data)


def get_args():
    parser = ArgumentParser()
    parser.add_argument("type", choices=["server", "client"], help="kind of service")
    return parser.parse_args()


if __name__ == "__main__":
    args = get_args()
    if args.type == "server":
        try:
            server()
        except KeyboardInterrupt:
            pass
    elif args.type == "client":
        client()
  • You can use a context-manager. socket() supports it like open()
  • Encode messages before you send
  • Decode messages early
  • There is the possibility of a UnicodeDecodeError.
  • Additional trick with tuple unpacking:
    one, (two, three) = (1, (2, 3))
  • You can have both in the same code, if you're using functions. In Addition ArgumentParser makes nice command line tools.



RE: TCP Communicate... - ATARI_LIVE - Dec-15-2020

THANKS FOR EXTRA!