Blog Cover Image

Inspire you to have New thinking, Walk out your unique Road.

[DEVELOP]PYTHON 桌機版開發之(5)

Posted onMay 9, 2018

程式,終於完成 80%,進入最終測試階段了

還來不及 update,日子就一天一天的過

約莫不知道哪天,搞定了 server 端接收後

其實還卡了一個問題,卡很久,但我決定不想卡在這,先轉攻 client 端的部分

這個問題就是

利用網站傳檔在傳到後台資料庫會發生無法讀取繁體字的狀況

中文就是這樣,至於設 utf-8 的問題,我也是卡很久,小學弟有辦法解決,但我到目前還是不知道怎麼解決

唯一解決的方法,我就擺到後面去


我繼續將我可能有使用到的網站給擷取了下來,但後續開發的動作,有沒有參考我記得我是沒有參考到的。

https://stackoverflow.com/questions/1046656/an-integer-is-required-open

http://web.ntnu.edu.tw/~samtseng/present/Python_and_Chinese.pdf

https://stackoverflow.com/questions/19591458/python-reading-from-a-file-and-saving-to-utf-8/19591815

https://www.fyears.org/2015/06/electron-as-gui-of-python-apps.html

http://www.runoob.com/python/python-gui-tkinter.html


https://www.reddit.com/r/django/comments/2grsay/how_to_read_csv_file_from_memory/


https://stackoverflow.com/questions/19130958/what-does-it-mean-to-call-a-function-in-python

https://programminghistorian.org/lessons/output-data-as-html-file

https://www.geeksforgeeks.org/get-post-requests-using-python/

2.7

https://docs.python.org/2.7/library/httplib.html?highlight=http%20client

3.6

https://docs.python.org/3/library/http.client.html

http://www.01happy.com/python-httplib-get-and-post/


回來開發桌機端後

我自己是在上一週想用 python 做一個生日快樂祝福視窗給人的時候,發現其實根本不用介面,也可以達到我要的結果

節省開發及研究的時間+可以達成功效

所以我就沒有繼續開發 Tkinter

python 的 .py 檔可以直接打包成 exe

如果不需介面的話,可以考慮使用這個方式

裡面的內容,我就很簡單的寫一寫

大致上就是將所有的函數 def()全部擺在最上面

然後主要使用 input, print, if, while, for 迴圈

還有一些學習到新的程式寫法

來分享一下


import os
os._exit(0)

def slowprint(s):
    for c in s + '\n':
        sys.stdout.write(c)
        sys.stdout.flush() # defeat buffering
        time.sleep(random.random() * 0.3)

更改 0.3 數字可以更改打印字的速度

slowprint('Hello, 各位聰明的同學')

這個是看要在哪下指令


import os
os.system("路徑/程式")


https://blog.csdn.net/suibianshen2012/article/details/51378948


No mudule name requests and Requirement already satisfied 解法

https://stackoverflow.com/questions/45345377/python-module-not-found-even-though-requirement-already-satisfied-in-pip


由於是簡易的,所以本來我想使用

httplib 來處理我上傳 post/get

但後來我發現,用簡單的方法就可以做到我用 Post, get 回傳到我的 server 端

Server 端是用 Django 的 View 來接受做處理,然後再回傳到我的後台跟 Vm

用 python 的 requests 做發送

Server 端則是用 Django 的 View 做處理

這邊是 Client 端簡單的寫法

import os
import requests
readfile = open('ooo.csv', 'rb')
my_files = {'upload_file': readfile }
my_data = {'studentID': '123','upload_file': readfile.read()}
r = requests.post('http://ooo.ooo.ooo.ooo/upload/',data=my_data)

Server 那邊,有作更動過,但因為我想將 Server 端跟 Client 端分開

所以,應該在準備連結的時候,回去將 Server 端改一改,再發篇文寫 Server 端的事

基本上就寫這樣 XD

剛好有處理回傳字串+檔案

而這邊剛好也是我上面提到卡繁體字的問題

解決方法就是用 rb, 用 binary 的方式讀檔案,壓成字串,再丟到 Server 用 binary 的方法寫檔案接著再回傳到雲端的資料夾裡,這是目前找到的方式。

readfile = open('ooo.csv', 'rb')

有 open 跟 with open 的方法

然後後面 rb 有代表的字

http://bigpxuan.blogspot.tw/2016/07/python-open.html

這個大大的文章中有解釋

大概是這樣

所以我桌機的程式就是之後壓成 exe 扔到 windows 做客戶端

不過卡了一個大問題就是我發現我做的程式會被掃毒擋

大概長這樣

程式碼的話,大致上架構是

# -*- coding: utf-8 -*-
import os
import sys
import requests
#import module


#definition function

def slowprint(s):
 for c in s + '\n':
 sys.stdout.write(c)
 sys.stdout.flush() # defeat buffering
 time.sleep(random.random() * 0.3)

def mydata(message):
 mydata = message
 upload_data = {'data': 'mydata'}
 r = requests.post('http://ooo.ooo.ooo.ooo/upload/',data=upload_data)

def ...


#your program begin

slowprint('...')
slowprint('...')
slowprint('...')


#Input
slowprint('請輸入你的學號:')
message = input("input:")
if ...
    mydata()
else:
    print("...")

...
...

大約就是這樣囉~~

目前就等兩個外部程式功能搞定,跟打包後

然後回去更改 Server 端好了後,就可以進入最終測試階段

希望這週可以搞定

然後跟教授說,下週就開始執行系統啦~


來記錄一下明天要做的事

  1. 將子程式打包並做測試(將收取的資料筆做更改)

  2. 等待第二個子程式做好

  3. 更改 Server 端程式碼(將接收動作搞定並分成兩個網段接收)

  4. 測試 Server 端跟 Client 端連結傳資料

  5. (看可不可以新增 ip)

  6. (看要如何將上傳檔案改名為學生學號)

  7. (有可能會需要做每 10 分鐘上傳的機制)


最後偷偷提醒一下

要在 windows 執行就要用 windows 打包程式

要在 mac 執行就要用 mac 打包

能不能跨平台我不知道

至少我用 mac 打包的程式是只有 mac 能執行~