0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ソケット通信

Last updated at Posted at 2019-12-18

ソケット通信

https://pc-karuma.net/windows-10-firewall-open-port/
https://memo.saitodev.com/home/python_network_programing/#id3

UDP

送信側

from __future__ import print_function
import socket
import time
from contextlib import closing

def main():
  host = 'XXX.XXX.XXX.XXX' #受信側のipv4
  port = 4000
  count = 0
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  with closing(sock):
    while True:
      message = 'Hello world : {0}'.format(count).encode('utf-8')
      print(message)
      sock.sendto(message, (host, port))
      count += 1
      
  return

if __name__ == '__main__':
  main()

受信側

from __future__ import print_function
import socket
from contextlib import closing

def main():
  host = 'XXX.XXX.XXX.XXX'  #受信側のipv4
  port = 4000
  bufsize = 4096

  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  with closing(sock):
    sock.bind((host, port))
    while True:
      print(sock.recv(bufsize))
  return

if __name__ == '__main__':
  main()

変換re

b = "b'19929717,168780'"
import re
regex = re.compile('\d+')
match = regex.findall(b)
print(match) #['19929717', '168780']
int(match[0]), int(match[1]) # (19929717, 168780)

動的更新

# !/usr/bin/env python
# -*- coding: utf8 -*-
import tkinter as tk
import random
def read_current_Temperature():
    return random.randint(1, 100)
def update():
    aktTemp.config(text=str(read_current_Temperature())+"°C")
    aktTemp.after(1000, update)
HEIGHT = 300
WIDTH = 500
root = tk.Tk()
canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
canvas.pack()
aktTemp = tk.Label(root, text=str(read_current_Temperature())+"°C", fg="red") 
aktTemp.pack()
update()
root.mainloop()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?