Created
December 18, 2024 10:17
-
-
Save alecordev/8820cf9990daca8d3083efcaa36d1de6 to your computer and use it in GitHub Desktop.
Python simple socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
local_hostname = socket.gethostname() | |
ip_address = socket.gethostbyname(local_hostname) | |
server_address = (ip_address, 23456) | |
sock.connect(server_address) | |
print('Connecting') | |
temperature_data = ['15', '22', '25'] | |
for point in temperature_data: | |
print(f'data: {point}') | |
new_data = str(point).encode('utf-8') | |
sock.sendall(new_data) | |
time.sleep(2) | |
sock.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
local_hostname = socket.gethostname() | |
local_fqdn = socket.getfqdn() | |
ip_address = socket.gethostbyname(local_hostname) | |
print(f'Working on {local_hostname}') | |
server_address = (ip_address, 23456) | |
sock.bind(server_address) | |
sock.listen(1) | |
while 1: | |
print('Waiting for connection') | |
connection, client_address = sock.accept() | |
try: | |
print('Connection from', client_address) | |
while 1: | |
data = connection.recv(64) | |
if data: | |
print(f'Data {data}') | |
else: | |
print('No more data') | |
break | |
finally: | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment