ПР2.3: TCP, UDP, HTTP, requests, Wireshark
This commit is contained in:
commit
6c9a514f67
0
.gitignore
vendored
Normal file
0
.gitignore
vendored
Normal file
0
api_git_example.py
Normal file
0
api_git_example.py
Normal file
17
http_socket_client.py
Normal file
17
http_socket_client.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
client.connect(("vyatsu.ru", 80))
|
||||||
|
|
||||||
|
request = (
|
||||||
|
"GET / HTTP/1.1\r\n"
|
||||||
|
"Host: vyatsu.ru\r\n"
|
||||||
|
"Connection: close\r\n"
|
||||||
|
"\r\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
client.sendall(request.encode("utf-8"))
|
||||||
|
response = client.recv(4096)
|
||||||
|
print(response.decode(errors="ignore"))
|
||||||
|
|
||||||
|
client.close()
|
||||||
4
requests_example.py
Normal file
4
requests_example.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import requests
|
||||||
|
|
||||||
|
response = requests.get("http://vyatsu.ru")
|
||||||
|
print(response.text[:500])
|
||||||
12
tcp_client.py
Normal file
12
tcp_client.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
client.connect(("127.0.0.1", 12000))
|
||||||
|
|
||||||
|
message = "hello server"
|
||||||
|
client.sendall(message.encode())
|
||||||
|
|
||||||
|
data = client.recv(1024)
|
||||||
|
print(f"Ответ от сервера: {data.decode()}")
|
||||||
|
|
||||||
|
client.close()
|
||||||
30
tcp_server.py
Normal file
30
tcp_server.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
server.bind(("0.0.0.0", 12000))
|
||||||
|
server.listen(1)
|
||||||
|
|
||||||
|
print("TCP сервер запущен и ожидает подключения...")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
conn, addr = server.accept()
|
||||||
|
print(f"Подключение от {addr}")
|
||||||
|
|
||||||
|
data = conn.recv(1024)
|
||||||
|
if not data:
|
||||||
|
conn.close()
|
||||||
|
continue
|
||||||
|
|
||||||
|
message = data.decode()
|
||||||
|
print(f"Получено сообщение: {message}")
|
||||||
|
|
||||||
|
response = message.upper()
|
||||||
|
conn.sendall(response.encode())
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if response == "EXIT":
|
||||||
|
print("Сервер завершает работу")
|
||||||
|
break
|
||||||
|
|
||||||
|
server.close()
|
||||||
8
token.py
Normal file
8
token.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import os
|
||||||
|
import requests
|
||||||
|
|
||||||
|
TOKEN = os.getenv("GIT_VYATSU_TOKEN") # токен через переменную окружения
|
||||||
|
headers = {"Authorization": f"token {TOKEN}"}
|
||||||
|
|
||||||
|
response = requests.get("https://git.vyatsu.ru/api/v1/user", headers=headers)
|
||||||
|
print(response.json())
|
||||||
11
udp_client.py
Normal file
11
udp_client.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
|
||||||
|
message = "привет сервер"
|
||||||
|
client.sendto(message.encode(), ("127.0.0.1", 10001))
|
||||||
|
|
||||||
|
data, _ = client.recvfrom(1024)
|
||||||
|
print(f"Ответ от сервера: {data.decode()}")
|
||||||
|
|
||||||
|
client.close()
|
||||||
14
udp_server.py
Normal file
14
udp_server.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import socket
|
||||||
|
|
||||||
|
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
server.bind(("0.0.0.0", 10001))
|
||||||
|
|
||||||
|
print("UDP сервер запущен и ожидает сообщения...")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
data, addr = server.recvfrom(1024)
|
||||||
|
message = data.decode()
|
||||||
|
print(f"Сообщение от {addr}: {message}")
|
||||||
|
|
||||||
|
response = message.upper()
|
||||||
|
server.sendto(response.encode(), addr)
|
||||||
Loading…
Reference in New Issue
Block a user