1
0
Fork 0

test app with python.

main
Gökmen Görgen 2 months ago
parent 57fe4d7eb3
commit 2feb8a500c
  1. 27
      .pre-commit-config.yaml
  2. 8
      poetry.lock
  3. 19
      pyproject.toml
  4. 0
      yank/__init__.py
  5. 39
      yank/client.py
  6. 38
      yank/server.py

@ -0,0 +1,27 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
- id: check-docstring-first
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.2.0
hooks:
- id: forbid-crlf
- id: remove-crlf
- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black

8
poetry.lock generated

@ -0,0 +1,8 @@
package = []
[metadata]
lock-version = "1.1"
python-versions = "^3.9"
content-hash = "ce2aa767160f871dd3652615ba0a0dceb7733d62eb8cb4665b87f30a562e3adf"
[metadata.files]

@ -0,0 +1,19 @@
[tool.poetry]
name = "yank"
version = "0.1.0"
description = ""
authors = ["Gökmen Görgen <gkmngrgn@gmail.com>"]
license = "MIT"
[tool.poetry.scripts]
server = "yank.server"
client = "yank.client"
[tool.poetry.dependencies]
python = "^3.9"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

@ -0,0 +1,39 @@
import socket
def get_constants(prefix):
"""Create a dictionary mapping socket module constants to their names."""
return dict((getattr(socket, n), n) for n in dir(socket) if n.startswith(prefix))
families = get_constants("AF_")
types = get_constants("SOCK_")
protocols = get_constants("IPPROTO_")
# create a tcp/ip socket
server_host = "localhost"
server_port = 10000
server_address = (server_host, server_port)
sock = socket.create_connection((server_host, server_port))
print(f"Family : {families[sock.family]}")
print(f"Type : {types[sock.type]}")
print(f"Protocol: {protocols[sock.proto]}")
print()
try:
# send data
message = b"This is the message. It will be repeated."
sock.sendall(message)
# look for the response
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
print(f"received {data}")
finally:
sock.close()

@ -0,0 +1,38 @@
import socket
import sys
# create tcp/ip socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to the port
server_host = "localhost"
server_port = 10000
server_address = (server_host, server_port)
print(f"starting up on {server_host} port {server_port}", file=sys.stderr)
sock.bind(server_address)
# listen for incoming connections
sock.listen(1)
while True:
print("waiting for a connection", file=sys.stderr)
connection, client_address = sock.accept()
try:
print(f"connection from {client_address}", file=sys.stderr)
# receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print(f"received {data}", file=sys.stderr)
if data:
print(f"sending data back to the client", file=sys.stderr)
connection.sendall(data)
else:
print(f"no more data from {client_address}")
break
finally:
# clean up the connection
connection.close()
Loading…
Cancel
Save