私は、Python 3とFancyURLopenerを使ってhttps経由でログインするスクリプトを持っています。 だから私はこれをやっている:python 3 https投稿が非常に遅い
from urllib.request import FancyURLopener, urlopen
from urllib.parse import urlencode
import re, sys
class SMS:
login_url = "url1"
login_act = "url2"
comp_url = "url3"
comp_act = "url4"
LEN = 100
def __init__(self, phone_num, password):
self.phone_num = phone_num
self.password = password
self.opener = FancyURLopener()
resp = self.opener.open(SMS.login_url)
cookie = ""
for x, y in resp.headers.items():
if x == "Set-Cookie":
cookie += y + "; "
cookie = cookie[:-2]
self.opener.addheader("Cookie", cookie)
print("--login")
self.login()
def login(self):
di = { "action":"", "refid":"", "continuation":"",\
"username":self.phone_num, "password":self.password, "image.x":"45", "image.y":"18"}
resp = self.opener.open(SMS.login_act, urlencode(di))
def send(self, to, message):
print("--sending")
if len(message) > 100:
print("Message length too long; Not send.")
return -1
resp = self.opener.open(SMS.comp_url).read().decode("windows-1251")
pattern1 = "<input type=\"hidden\" name=\"daycreditsmsleft\" value=\"(\d+)\""
pattern2 = "<input type=\"HIDDEN\" name=\"smssenttime\" value=\"(\d+)\""
post = {"brand":"", "daycreditsmsleft":re.search(pattern1, resp).groups()[0],\
"process":"true", "btnSendSMS.x":"75", "btnSendSMS.y":"23", "model":"0",\
"smssenttime":re.search(pattern2, resp).groups()[0], "remainingChars": str(SMS.LEN - len(message)),\
"reply2inbox":"false", "selReceiverName":"", "txtareaMessage":message, "receiverPhoneNum":to}
if post["daycreditsmsleft"] == 0:
print("No more messages; Not send")
return -1
resp = self.opener.open(SMS.comp_act, urlencode(post))
私は、このようにそれを使用します。問題は、POSTリクエストが遅すぎるということです
s = SMS("telephone_num", "pass")
s.send(tel, message)
。ログインには20秒以上かかります。 スクリプト全体に2つのPOST要求があり、実際には1分近く実行されます。どのように最適化する?
また、プログラムが終了する前にメッセージが受信されていることに気付きました。
「遅すぎますか?手動で(ブラウザを使用して)ログインしようとしましたか?それはもっと速いですか?どれくらい早い?マニュアルと 'urllib 'の時間比較を投稿できますか? –
可能であれば、これを再現するためにすべての詳細(Cookie、ユーザー名、パスワード、およびURLの例)を入力してください。この問題は、あなたが提供したコードの中にはないかもしれませんが、私はこれが未回答のままであるかもしれないという疑いがあります。 – marcog
ブラウザを使用するとすぐにログに記録されます。すべてのコードを追加します。 – Marii