from urllib.request import urlopen, Request from urllib.parse import urlencode from glob import glob def getPoem(fn): with open(fn, 'r') as infile: title = infile.readline()[:-1] author = infile.readline()[:-1] line = infile.readline()[:-1] # blank if line != '': print('[Warning] In "{}", the 3rd line is not blank: {}'.format( fn, line)) poem = infile.read() return title, author, poem def main(): url = 'http://kghs.ncnu.net:2000/post' print('''(1) 2王昱婷、30楊蕎宇、37謝雨彤 (2) 3王紫婷、13林沛玗、21郭倢妤、28黃品薰 (3) 10沈宥羽、11沈樂甯、25黃宇卉、33歐鎧綺 (4) 5朱宥蓁、17高翊婷、29楊鈞喻 (5) 31葉家妤、34蔡旻芸、35鄭羽辰、36謝念岑 (6) 14林莉芸、18張瑀芯、20郭予恩、24陳曉晴 (7) 8吳瑜珍、9李奕璇、15姜卉秝 (8) 6吳泯蓁、7吳苡柔、26 黃宇瑄、32劉靜儀''') teamNo = int(input("What is your team number? ")) count = 0 for fn in sorted(glob('poem*.txt')): title, author, poem = getPoem(fn) aDict = { "teamNo": teamNo, "title": title, "author": author, "poem": poem } req = Request(url, data=urlencode(aDict).encode('UTF-8')) count += 1 print("{}. {}".format(count, title)) html = urlopen(req).read().decode() main()