[PYTHON] How to create a simple TCP server / client script

Introduction

Created because it can be used for simple communication confirmation between servers. The book I referred to was python2, so it didn't work subtly. I will upload a slightly modified version for python3.

By the way, the execution assumption is

Execution environment

・ Python 3.7.6

Execution procedure

① Create TCPserver.py and TCPclient.py on the same HOST ② Execute TCPserver.py from the terminal ③ Start another terminal and execute TCPclient.py

TCP client

TCPclient.py


import socket

target_host="127.0.0.1"
target_port=9999

#create socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect to object
client.connect((target_host, target_port))

# data transmission
client.send(b"ABCDEFG")

#recieve data
response = client.recv(4096)
print(response)

TCP server

TCPserver.py



import socket
import threading

bind_ip = "0.0.0.0"
bind_port = 9999

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server.bind((bind_ip, bind_port))

server.listen(5)

print("[*] Listening on %s:%d" % (bind_ip, bind_port))

def handle_client(client_socket):
    request = client_socket.recv(1024)
    print("[*] Recieved: %s:" % request)
    client_socket.send("ACK!")
    client_socket.close()

while True:
    client,addr = server.accept()

    print("[*] Accepted connection from: %s:%d" % (addr[0],addr[1]))

    client_handler = threading.Thread(target=handle_client, args=(client,))
    cliend_handler.start()

Execution result

左側がTCPserver.py 右側がTCPclient.py Execution result of. コメント 2020-05-04 143049.png

in conclusion

I feel that this amount of code is enough if I just connect it. I also learned how easy it is if the server and client match the ports. Speaking of textbooks, that's right, but when you look at what works with the code This impresses me with this.

Recommended Posts

How to create a simple TCP server / client script
Overview of how to create a server socket and how to establish a client socket
Create a (simple) REST server
Create a simple textlint server
How to create a Conda package
How to create a virtual bridge
How to create a Dockerfile (basic)
Write a super simple TCP server
How to create a config file
[Python Kivy] How to create a simple pop up window
How to create a clone from Github
How to create a git clone folder
[Ubuntu] How to execute a shell script
How to create a repository from media
Script to create a Mac dictionary file
How to run a Maya Python script
Verification of how to periodically execute a script on a Linux server on Windows
A simple example of how to use ArgumentParser
How to create a Python virtual environment (venv)
How to create a function object from a string
How to create a shortcut command for LINUX
[Note] How to create a Ruby development environment
How to create a Kivy 1-line input box
How to set up a local development server
How to create a multi-platform app with kivy
How to create a Rest Api in Django
[Note] How to create a Mac development environment
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
[Go] How to create a custom error for Sentry
How to create a local repository for Linux OS
Python script to create a JSON file from a CSV file
[Python] How to create a 2D histogram with Matplotlib
How to run Django on IIS on a Windows server
How to create a kubernetes pod from python code
Script to easily create a client device environment for AWS IoT (Python v2 version)
How to use NUITKA-Utilities hinted-compilation to easily create an executable file from a Python script
How to hack a terminal
How to use GitHub on a multi-person server without a password
How to run a Python program from within a shell script
How to launch AWS Batch from a python client app
How to create a flow mesh around a cylinder with snappyHexMesh
How to make a simple Flappy Bird game with pygame
I'll never forget how to write a shell script, don't forget! !!
How to create a SAS token for Azure IoT Hub
How to set up a simple SMTP server that can be tested locally in Python
How to make a Japanese-English translation
How to write a Python class
Introduction to Socket API Learned in C Part 3 TCP Server / Client # 1
[C language] [Linux] Try to create a simple Linux command * Just add! !!
How to put a symbolic link
How to pass arguments to a Python script in SPSS Modeler Batch
A note on how to check the connection to the license server port
How to build a NEM (current version) node (NIS1: NEM Infrastructure Server)
Steps to create a Django project
Create a shell script to run the python file multiple times
How to make a slack bot
How to create your own Transform
How to create an email user
Rails users try to create a simple blog engine with Django
How to make a crawler --Advanced
How to make a recursive function