2022年1月12日 星期三
Python socket基本使用
Python socket基本使用
三次握手協定的過程:
- 客戶端(通過執行connect函式)向伺服器端傳送一個SYN包,請求一個主動打開。該包攜帶客戶端為這個連接請求而設定的亂數A作為訊息序列號。
- 伺服器端收到一個合法的SYN包後,把該包放入SYN佇列中;回送一個SYN/ACK。ACK的確認碼應為A+1,SYN/ACK包本身攜帶一個隨機產生的序號B。
- 客戶端收到SYN/ACK包後,傳送一個ACK包,該包的序號被設定為A+1,而ACK的確認碼則為B+1。然後客戶端的connect函式成功返回。當伺服器端收到這個ACK包的時候,把請求影格從SYN佇列中移出,放至ACCEPT佇列中;這時accept函式如果處於阻塞狀態,可以被喚醒,從ACCEPT佇列中取出ACK包,重新建立一個新的用於雙向通信的sockfd,並返回。
1)socket函式
功能:使用給定的地址族、套接字型別、協議編號(預設為0)來建立套接字。
格式:socket.socket([family[, type[, proto]]])
引數:
family : AF_INET (預設ipv4),AF_INET6(ipv6) , AF_UNIX(Unix系統程序間通訊).
type : SOCK_STREAM (TCP), SOCK_DGRAM(UDP) .
protocol : 一般為0或者預設
備註:如果socket建立失敗會丟擲一個socket.error異常
2)伺服器端函式
a)bind函式
格式:s.bind(address)
功能:將地址address繫結到套接字, 地址以元組(host,port)的形式表示。
引數:
address為元組(host,port)
host: ip地址, 為一個字串
post: 自定義主機號, 為整型
b)listen函式
格式:s.listen(backlog)
功能:使伺服器的這個埠和IP處於監聽狀態,等待網路中某一客戶機的連線請求。如果客戶端有連線請求,埠就會接受這個連線。
引數:backlog : 作業系統可以掛起的最大連線數量。該值至少為1,大部分應用程式設為5就可以了
c)accept函式
格式:s.accept()
功能:接受遠端計算機的連線請求,建立起與客戶機之間的通訊連線。伺服器處於監聽狀態時,如果某時刻獲得客戶機的連線請求,此時並不是立即處理這個請求,而是將這個請求放在等待佇列中,當系統空閒時再處理客戶機的連線請求。
返回值:返回一個數組(conn,address),其中conn是新的套接字物件,可以用來接收和傳送資料。address是連線客戶端的地址
3)客戶端函式
a)connect函式
格式:s.connect(address)
功能:用來請求連線遠端伺服器
引數:address為遠端伺服器地址, 格式為元組(hostname,port),如果連接出錯,返回socket.error錯誤
b)connect_ex函式
格式:s.connect_ex(address)
備註:connect()函式的擴充套件版本,出錯時返回出錯碼,而不是丟擲異常
4)通用函式
a)recv函式
格式:s.recv(bufsize[,flag])
功能:接收遠端主機傳來的資料
引數:
bufsize : 指定要接收的資料大小
flag : 提供有關訊息的其他資訊,通常可以忽略
返回值:返回值為資料以字串形式</code>
b)send函式
格式:s.send(string[,flag])
功能:傳送資料給指定的遠端主機
引數:
string : 要傳送的字串資料
flag : 提供有關訊息的其他資訊,通常可以忽略
返回值:返回值是要傳送的位元組數量,該數量可能小於string的位元組大小。
c)sendall函式
格式:s.sendall(string[,flag])
功能:內部呼叫了send函式,完整發送TCP資料。將string中的資料傳送到連線的套接字,但在返回之前會嘗試傳送所有資料。
引數:同send函式
返回值 : 成功返回None,失敗則丟擲異常。
d)close函式
格式:s.close()
功能:關閉套接字
e)recvfrom函式
格式:s.recvfrom(bufsize[.flag])
功能:與recv()類似,區別是返回值不同
返回值:返回一個數組(data,address),其中data是包含接收資料的字串,address是傳送資料的套接字地址。
f)sendto函式
格式:s.sendto(string[,flag],address)
功能:將資料傳送到套接字
引數:
string : 要傳送的字串資料
flag : 提供有關訊息的其他資訊,通常可以忽略
address是形式為(ipaddr,port)的元組,指定遠端地址
返回值:返回值是要傳送的位元組數量
備註:該函式主要用於UDP協議。
g)settimeout函式
格式:s.settimeout(timeout)
功能:設定套接字操作的超時期
引數:timeout是一個浮點數,單位是秒。值為None表示沒有超時期。一般,超時期應該在剛建立套接字時設定,因為它們可能用於連線的操作(如 client 連線最多等待5s )
h)getpeername函式
格式:s.getpeername()
功能:獲取連線套接字的遠端地址
返回值:返回值通常是元組(ipaddr,port)。
i)getsockname函式
格式:s.getsockname()
功能:獲取套接字自己的地址
返回值:通常是一個元組(ipaddr,port)
socket中常用的函式就上面這些了。先用上面這些函式嘗試TCP協議下的socket通訊。
import socket
sk = socket.socket()
sk.connect(('127.0.0.1',8088))
mes_to_server = '你好,Linux公社www.linuxidc.com伺服器!'.encode('utf-8')#傳送的資料必須是byte型別
sk.send(mes_to_server)
mes_from_server = sk.recv(1024).decode('utf-8')
print(mes_from_server)
sk.close()
import socket
sk = socket.socket()
sk.bind(('127.0.0.1' ,8088))
sk.listen(5)
print('正在等待Linux公社客戶端連線……')
conn , addr = sk.accept()
print('Linux公社客戶端已連線到伺服器……')
mes_from_client = conn.recv(1024).decode('utf-8')
print(mes_from_client)
mes_to_server = '你好,Linux公社www.linuxidc.com客戶端,已收到您的資訊!'.encode('utf-8')#傳送的資料必須是byte型別
conn.send(mes_to_server)
conn.close()
sk.close()
2022年1月9日 星期日
Python Tkinter Control Arduino LED
Python Tkinter Control Arduino LED
需使用Virtual Seria;l Ports Emulator
模擬Pair 方式 COM5<-->COM7
(Python Tkinter Com port = 7)
import serial
import time
import tkinter
def quit():
global tkTop
ser.write(bytes('L', 'UTF-8'))
tkTop.destroy()
def set_button1_state():
global b
b += 1
varLabel.set("LED ON ")
ser.write(bytes('H', 'UTF-8'))
varLabel2.set(b)
print(b)
def set_button2_state():
varLabel.set("LED OFF")
ser.write(bytes('L', 'UTF-8'))
ser = serial.Serial('com7', 9600)
print("Reset Arduino")
time.sleep(3)
ser.write(bytes('L', 'UTF-8'))
tkTop = tkinter.Tk()
tkTop.geometry('300x200')
tkTop.title("IoT24hours")
label3 = tkinter.Label(text = 'Building Python GUI to interface an arduino,'
'\n and control an LED',font=("Courier", 12,'bold')).pack()
tkTop.counter = 0
b = tkTop.counter
varLabel = tkinter.IntVar()
tkLabel = tkinter.Label(textvariable=varLabel, )
tkLabel.pack()
varLabel2 = tkinter.IntVar()
tkLabel2 = tkinter.Label(textvariable=varLabel2, )
tkLabel2.pack()
button1 = tkinter.IntVar()
button1state = tkinter.Button(tkTop,
text="ON",
command=set_button1_state,
height = 4,
fg = "black",
width = 8,
bd = 5,
activebackground='green'
)
button1state.pack(side='top', ipadx=10, padx=10, pady=15)
button2 = tkinter.IntVar()
button2state = tkinter.Button(tkTop,
text="OFF",
command=set_button2_state,
height = 4,
fg = "black",
width = 8,
bd = 5
)
button2state.pack(side='top', ipadx=10, padx=10, pady=15)
tkButtonQuit = tkinter.Button(
tkTop,
text="Quit",
command=quit,
height = 4,
fg = "black",
width = 8,
bg = 'yellow',
bd = 5
)
tkButtonQuit.pack(side='top', ipadx=10, padx=10, pady=15)
tkinter.mainloop()
int incomingByte; // variable stores serial data
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
Serial.println("Getting H"); //print out to serial monitor to check state
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
Serial.println("Getting L"); //print out to serial monitor to check state
}
}
}
2022年1月8日 星期六
Python 簡易計算機
Python 簡易計算機
# Python program to create a simple GUI
# calculator using Tkinter
# import everything from tkinter module
from tkinter import *
# globally declare the expression variable
expression = ""
def Exit():
sys.exit()
# Function to update expression
# in the text entry box
def press(num):
# point out the global expression variable
global expression
# concatenation of string
expression = expression + str(num)
# update the expression by using set method
equation.set(expression)
# Function to evaluate the final expression
def equalpress():
# Try and except statement is used
# for handling the errors like zero
# division error etc.
# Put that code inside the try block
# which may generate the error
try:
global expression
# eval function evaluate the expression
# and str function convert the result
# into string
total = str(eval(expression))
equation.set(total)
# initialize the expression variable
# by empty string
expression = ""
# if error is generate then handle
# by the except block
except:
equation.set(" error ")
expression = ""
# Function to clear the contents
# of text entry box
def clear():
global expression
expression = ""
equation.set("")
# Driver code
if __name__ == "__main__":
# create a GUI window
gui = Tk()
# set the background colour of GUI window
gui.configure(background="light green")
# set the title of GUI window
gui.title("Simple Calculator")
# set the configuration of GUI window
gui.geometry("402x285")
# StringVar() is the variable class
# we create an instance of this class
equation = StringVar()
# create the text entry box for
# showing the expression .
expression_field = Entry(gui, textvariable=equation)
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
expression_field.grid(columnspan=4, ipadx=70)
# create a Buttons and place at a particular
# location inside the root window .
# when user press the button, the command or
# function affiliated to that button is executed .
button1 = Button(gui, text=' 1 ', fg='black', bg='red',
command=lambda: press(1), height=2, width=10)
button1.grid(row=2, column=0)
button2 = Button(gui, text=' 2 ', fg='black', bg='red',
command=lambda: press(2), height=2, width=10)
button2.grid(row=2, column=1)
button3 = Button(gui, text=' 3 ', fg='black', bg='red',
command=lambda: press(3), height=2, width=10)
button3.grid(row=2, column=2)
button4 = Button(gui, text=' 4 ', fg='black', bg='red',
command=lambda: press(4), height=2, width=10)
button4.grid(row=3, column=0)
button5 = Button(gui, text=' 5 ', fg='black', bg='red',
command=lambda: press(5), height=2, width=10)
button5.grid(row=3, column=1)
button6 = Button(gui, text=' 6 ', fg='black', bg='red',
command=lambda: press(6), height=2, width=10)
button6.grid(row=3, column=2)
button7 = Button(gui, text=' 7 ', fg='black', bg='red',
command=lambda: press(7), height=2, width=10)
button7.grid(row=4, column=0)
button8 = Button(gui, text=' 8 ', fg='black', bg='red',
command=lambda: press(8), height=2, width=10)
button8.grid(row=4, column=1)
button9 = Button(gui, text=' 9 ', fg='black', bg='red',
command=lambda: press(9), height=2, width=10)
button9.grid(row=4, column=2)
button0 = Button(gui, text=' 0 ', fg='black', bg='red',
command=lambda: press(0), height=2, width=10)
button0.grid(row=5, column=0)
plus = Button(gui, text=' + ', fg='black', bg='red',
command=lambda: press("+"), height=2, width=10)
plus.grid(row=2, column=3)
minus = Button(gui, text=' - ', fg='black', bg='red',
command=lambda: press("-"), height=2, width=10)
minus.grid(row=3, column=3)
multiply = Button(gui, text=' * ', fg='black', bg='red',
command=lambda: press("*"), height=2, width=10)
multiply.grid(row=4, column=3)
divide = Button(gui, text=' / ', fg='black', bg='red',
command=lambda: press("/"), height=2, width=10)
divide.grid(row=5, column=3)
equal = Button(gui, text=' = ', fg='black', bg='red',
command=equalpress, height=2, width=10)
equal.grid(row=5, column=2)
clear = Button(gui, text='Clear', fg='black', bg='red',
command=clear, height=2, width=10)
clear.grid(row=5, column=1)
Decimal= Button(gui, text='.', fg='black', bg='red',
command=lambda: press('.'), height=2, width=10)
Decimal.grid(row=6, column=0)
Exit = Button(gui, text='Exit', fg='black', bg='blue',
command=Exit, height=2, width=10)
Exit.grid(row=6, column=1)
# start the GUI
gui.mainloop()
Python Tkinter widget
Python tkinter 提供了下列 21 種 GUI 元件, 稱為 widget (視窗元件) 或 control (控制項), 所謂 widget 乃 Window gadget 之意 :
- Label (標籤)
- Button (按鈕)
- Radiobutton (單選圓鈕)
- Checkbutton (核取方塊)
- Entry (文字欄位)
- Frame (框)
- LabelFrame (文字框)
- Listbox (清單方塊)
- Text (文字框)
- Message
- PanedWindow
- Scrollbar (捲軸)
- Scale
- Spinbox
- Menu (選單)
- OptionMenu
- Menubutton (選單按鈕)
- Canvas (畫布)
- Image (圖片)
- Bitmap (點陣圖)
- Toplevel
Python-Tkinter Scrollbar
Python-Tkinter Scrollbar
w = Scrollbar(master, options)
Parameters:
- master: This parameter is used to represents the parent window.
- options: There are many options which are available and they can be used as key-value pairs separated by commas.
Options:
Following are commonly used Option can be used with this widget :-
- activebackground: This option is used to represent the background color of the widget when it has the focus.
- bg: This option is used to represent the background color of the widget.
- bd: This option is used to represent the border width of the widget.
- command: This option can be set to the procedure associated with the list which can be called each time when the scrollbar is moved.
- cursor: In this option, the mouse pointer is changed to the cursor type set to this option which can be an arrow, dot, etc.
- elementborderwidth: This option is used to represent the border width around the arrow heads and slider. The default value is -1.
- Highlightbackground: This option is used to focus highlighcolor when the widget doesn’t have the focus.
- highlighcolor: This option is used to focus highlighcolor when the widget has the focus.
- highlightthickness: This option is used to represent the thickness of the focus highlight.
- jump: This option is used to control the behavior of the scroll jump. If it set to 1, then the callback is called when the user releases the mouse button.
- orient: This option can be set to HORIZONTAL or VERTICAL depending upon the orientation of the scrollbar.
- repeatdelay: This option tells the duration up to which the button is to be pressed before the slider starts moving in that direction repeatedly. The default is 300 ms.
- repeatinterval: The default value of the repeat interval is 100.
- takefocus: You can tab the focus through a scrollbar widget
- troughcolor: This option is used to represent the color of the trough.
- width: This option is used to represent the width of the scrollbar.
Methods:
Methods used in this widgets are as follows:
- get(): This method is used to returns the two numbers a and b which represents the current position of the scrollbar.
- set(first, last): This method is used to connect the scrollbar to the other widget w. The yscrollcommand or xscrollcommand of the other widget to this method.
from tkinter import *
root = Tk()
root.geometry("150x200")
w = Label(root, text ='GeeksForGeeks',
font = "50")
w.pack()
scroll_bar = Scrollbar(root)
scroll_bar.pack( side = RIGHT,
fill = Y )
mylist = Listbox(root,
yscrollcommand = scroll_bar.set )
for line in range(1, 26):
mylist.insert(END, "Geeks " + str(line))
mylist.pack( side = LEFT, fill = BOTH )
scroll_bar.config( command = mylist.yview )
root.mainloop()
from tkinter import *
root = Tk()
root.title('PythonGuides')
root.geometry('400x300')
root.config(bg='#4A7A8C')
frame = Frame(root)
frame.pack(expand=True, fill=BOTH, padx=20, pady=20)
lb = Listbox(
frame,
font= (12)
)
lb.pack(expand=True, fill=BOTH, side=LEFT)
sb = Scrollbar(
frame,
orient=VERTICAL
)
sb.pack(fill=Y, side=RIGHT)
lb.configure(yscrollcommand=sb.set)
sb.config(command=lb.yview)
lb.insert(0, 'Fruits')
lb.insert(1,'Citrus – oranges, limes, grapefruits and mandarins')
lb.insert(2,'Stone fruit – apricots, plums, peaches and nectarines')
lb.insert(3,'Tropical and exotic – mangoes a bananasnd ')
lb.insert(4,'Berries – kiwifruit, raspberries, strawberries, blueberries, and passionfruit')
lb.insert(5,'Melons – honeydew melons, watermelons and rockmelons')
lb.insert(6, 'Vegetables')
lb.insert(7, 'Leafy green – spinach and silverbeet, lettuce')
lb.insert(8, 'Cruciferous – cabbage, broccoli, Brussels sprouts and cauliflower')
lb.insert(9, 'Marrow – zucchini, cucumber and pumpkin')
lb.insert(10, 'Root – yam, sweet potato and potato')
lb.insert(11, 'Edible plant stem – asparagus and celery')
lb.insert(12, 'Allium – garlic, onion and shallot')
root.mainloop()
from tkinter import *
root = Tk()
root.title('PythonGuides')
root.geometry('400x300')
root.config(bg='#4A7A8C')
frame = Frame(root)
frame.pack(expand=True, fill=BOTH, padx=20, pady=20)
lb = Listbox(
frame,
font= (12)
)
lb.pack(expand=True, fill=BOTH)
sb = Scrollbar(
frame,
orient=HORIZONTAL
)
sb.pack(fill=X)
lb.configure(xscrollcommand=sb.set)
sb.config(command=lb.xview)
lb.insert(0, 'Not all heros wear capes.')
lb.insert(1, 'Game changers are in blue')
lb.insert(2, 'With great power comes great responsibility')
lb.insert(3, 'A noun phrase has a noun or pronoun as the main word')
lb.insert(4, 'With great power comes great responsibility')
lb.insert(5, 'contribute to open source, as this will help & motivate you.')
root.mainloop()
清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...
-
Line 發報機 Python TKinter (CONFIG_LINE Message API_2.py) import serial import serial.tools.list_ports import tkinter as tk from tkinter import...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...