[PYTHON] Basics of network programs?

Assuming WebSocket communication, the basics of network programming are smooth. We plan to develop it based on the following.

Specifying the communication type (Tranport)

** Stream type ** I want to make connection-oriented communication that maintains the connection with TCP.

example


socket.socket(socket.AF_INET, socket.SOCK_STREAM)

** Datagram type ** I want to make connectionless communication that maintains the connection with UDP.

example


socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

About the port

With only IP, only one session of communication can be performed. Can multiple communication processes be performed using the IP + port? (Investigation required) Port number can be specified from 1 to 65535

Port num Des
1~1023 Known port number
1024~49151 The left is also registered
49152~65535 You can use it freely

[Wiki port number list](https://ja.wikipedia.org/wiki/TCP%E3%82%84UDP%E3%81%AB%E3%81%8A%E3%81%91%E3%82%8B % E3% 83% 9D% E3% 83% BC% E3% 83% 88% E7% 95% AA% E5% 8F% B7% E3% 81% AE% E4% B8% 80% E8% A6% A7)

How to make a server socket

1.socket Example) I want to use TCP communication

example


s.socket(socket.AF_INET, socket.SOCK_STREAM)

2.bind Fix it to the created socket with the ip and port to be used.

example


s.bind((HOST,PORT))

3.listen The process of enabling the created socket and accepting server connections. Specify the number of connectable items. More than the specified number should be rejected ...

example


s.listen(1)

4.accept Make it connectable.

example


conn, addr = s.accept()

5.send or recv Actual reception processing One reception size can be specified in Byte.

example


recv_mess = s.recv()

6.close Close

example


s.close()

How to make a client socket

1.socket Example) I want to use TCP communication

example


s.socket(socket.AF_INET, socket.SOCK_STREAM)

2.connect Connect to the target ip and port

example


s.connect((host,port))

3.send or recv

example


s.sendall(msg)

4.closesocket Close

example


s.close(msg)

Recommended Posts

Basics of network programs?
Basics of Python ①
Basics of python ①
[Python] Chapter 02-03 Basics of Python programs (input / output)
Basics of Python scraping basics
[Python] Chapter 02-01 Basics of Python programs (operations and variables)
# 4 [python] Basics of functions
Basics of Perceptron Foundation
Basics of regression analysis
[Python] Chapter 02-02 Basics of Python programs (Handling of character strings)
Basics of python: Output
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
[Python] Chapter 02-06 <Supplement> Basics of Python programs (handling of numerical values)
Basics of PyTorch (2) -How to make a neural network-
Basics of Machine Learning (Notes)
Static analysis of Python programs
python: Basics of using scikit-learn ①
Supervised learning 1 Basics of supervised learning (classification)
XPath Basics (1) -Basic Concept of XPath
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Paiza Python Primer 5: Basics of Dictionaries
Read "Basics of Quantum Annealing" Day 5
Implementation of a two-layer neural network 2
[Must-see for beginners] Basics of Linux
Topic extraction of Japanese text 1 Basics
Review of the basics of Python (FizzBuzz)
Basics of Quantum Information Theory: Entropy (2)
Basics of Python x GIS (Part 2)
Touch the object of the neural network
Basics of touching MongoDB with MongoEngine
network
Read "Basics of Quantum Annealing" Day 6
About the basics list of Python basics
Learn the basics of Python ① Beginners
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
Basics of Quantum Information Theory: Data Compression (1)
Learn the basics of Theano once again
[Learning memo] Basics of class by python
[Python3] Understand the basics of Beautiful Soup
Implementation of 3-layer neural network (no learning)
I didn't know the basics of Python
Basics of Quantum Information Theory: Horebaud Limits
Basics: Full Search of Tree Structure (DFS/BFS)
The basics of running NoxPlayer in Python
Basics of Tableau Basics (Visualization Using Geographic Information)
Implementation of "blurred" neural network using Chainer
Jupyter Notebook Basics of how to use
Basics of PyTorch (1) -How to use Tensor-
Basics of Quantum Information Theory: Trace Distance
[Linux] Learn the basics of shell commands
Gacha written in python-Practice 2 ・ Basics of step-up gacha-
[Python] Chapter 02-04 Basics of Python Program (About Comments)
Basics of Quantum Information Theory: Quantum State Tomography
Basics of Quantum Information Theory: Data Compression (2)
[Introduction to Data Scientists] Basics of Python ♬
[Python3] Understand the basics of file operations