Skip to content

Instantly share code, notes, and snippets.

@muzizongheng
Last active November 8, 2024 17:13
Show Gist options
  • Save muzizongheng/6019506 to your computer and use it in GitHub Desktop.
Save muzizongheng/6019506 to your computer and use it in GitHub Desktop.
login CSDN by using urllib
#Use this utility to access CSDN blog.
#Author: jiangong li
#Email: [email protected]
import base64
from http import cookiejar
import urllib.request, urllib.parse, urllib.error
#url for accessing
csdnLoginUrl = r"http://passport.csdn.net/ajax/accounthandler.ashx?"
moduleUrl = r"http://write.blog.csdn.net/postedit/"
csdnAccessModuleUrl = r"http://passport.csdn.net/account/loginbox?callback=logined&hidethird=1&from="+urllib.parse.quote(moduleUrl)#http%3a%2f%2fwrite.blog.csdn.net%2f"
#install cookie
cj = cookiejar.CookieJar();
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj));
urllib.request.install_opener(opener)
#build request for accessed url
homeReq = urllib.request.Request(
url = csdnAccessModuleUrl
)
homeReq.add_header('Accept', 'text/html, application/xhtml+xml, */*');
homeReq.add_header('Accept-Language', 'en-US')
homeReq.add_header('Accept-Encoding', 'gzip, deflate')
homeReq.add_header('Connection', 'Keep-Alive');
homeReq.add_header('Referer', 'http://passport.csdn.net/account/login?from='+urllib.parse.quote(moduleUrl))#http%3a%2f%2fwrite.blog.csdn.net%2f')
homeReq.add_header('User-Agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)');
#open access url
resp = urllib.request.urlopen(homeReq)
print(resp.info())
print(resp.status)
print(cj)
################################################################################
#build request for login url
#post data
postdata = {
'u':'yourUserName',
'p':'yourPassword',
'remember':'1',
't':'log',
'f':urllib.parse.quote(moduleUrl),
}
postdata = urllib.parse.urlencode(postdata).encode('utf-8')
print(postdata)
req = urllib.request.Request(
url = csdnLoginUrl,
data = postdata)
req.add_header('Accept', 'text/html, application/xhtml+xml, */*');
req.add_header('Accept-Language', 'en-US')
req.add_header('Accept-Encoding', 'gzip, deflate')
req.add_header('Connection', 'Keep-Alive');
req.add_header('Referer', csdnAccessModuleUrl)
req.add_header('User-Agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)');
#open login url
r = urllib.request.urlopen(req)
print(r.status)
print(r.reason)
print(r.geturl())
print(r.info())
data = r.read().decode('utf-8')
print(data)
print(cj)
@kiraio-moe
Copy link

Yes!, I picked a random france number from quackr but the message is never received. Futher debugging I realize that the request that sends the message returns a 521 status code.

Try different France number then. Someone might have been using the same phone number or likely it has been blocked by CSDN itself.

The VPN you use, is using China?. Its a guess that the location might be the problem, or maybe the website is trully down 😅 @kiraio-moe

I can't truly remember what server I use when I signed up at that time. I used USA, Singapore, France, UK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment