将一段python代码大概翻译成java
这是一段模拟登陆论坛的python代码代码如下classloginInfo(QtCore.QThread):def__init__(self,conn,parent):Q...
这是一段模拟登陆论坛的python代码
代码如下
class loginInfo(QtCore.QThread):
def __init__(self, conn, parent):
QtCore.QThread.__init__(self, parent)
self.conn = conn
self.username, self.password = self.conn.getUsernameAndPassword()
def run(self):
mutex2.lock()
self.status = self.getConnection()
mutex2.unlock()
def getConnection(self):
md5passwd = hashlib.md5(self.password).hexdigest()
self.passwd = md5passwd[8:24]
self.conn.setPasswd(self.passwd)
self.conn = httplib.HTTPConnection("www.cc98.org")
self.conn.putrequest("GET", "/index.asp")
self.conn.endheaders()
res = self.conn.getresponse()
res.read()
self.cookie = res.getheader("set-cookie")
sessionlist = re.findall(r"ASPSESSION.*(?=; path=/)", self.cookie)
session = sessionlist[0]
self.cookie = "BoardList=BoardID=Show; " + session
data = urllib.urlencode({"a":"i", "u":self.username, "p":md5passwd, "userhidden":"2"})
Content_Length = data.__len__()
self.conn.putrequest("GET", "/sign.asp")
self.conn.putheader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
self.conn.putheader("Cookie", self.cookie)
self.conn.putheader("Content-Length", str(Content_Length))
self.conn.endheaders()
self.conn.send(data)
res = self.conn.getresponse()
if res.read() != "9898":
return False
userinfo = re.findall(r"aspsky.*?(?=;\s)", res.getheader("set-cookie"))
self.cookie = "BoardList=BoardID=Show; upNum=0; " + session + "; " + userinfo[0]
return True
def getStatus(self):
return self.status
def getCookie(self):
return self.cookie 展开
代码如下
class loginInfo(QtCore.QThread):
def __init__(self, conn, parent):
QtCore.QThread.__init__(self, parent)
self.conn = conn
self.username, self.password = self.conn.getUsernameAndPassword()
def run(self):
mutex2.lock()
self.status = self.getConnection()
mutex2.unlock()
def getConnection(self):
md5passwd = hashlib.md5(self.password).hexdigest()
self.passwd = md5passwd[8:24]
self.conn.setPasswd(self.passwd)
self.conn = httplib.HTTPConnection("www.cc98.org")
self.conn.putrequest("GET", "/index.asp")
self.conn.endheaders()
res = self.conn.getresponse()
res.read()
self.cookie = res.getheader("set-cookie")
sessionlist = re.findall(r"ASPSESSION.*(?=; path=/)", self.cookie)
session = sessionlist[0]
self.cookie = "BoardList=BoardID=Show; " + session
data = urllib.urlencode({"a":"i", "u":self.username, "p":md5passwd, "userhidden":"2"})
Content_Length = data.__len__()
self.conn.putrequest("GET", "/sign.asp")
self.conn.putheader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
self.conn.putheader("Cookie", self.cookie)
self.conn.putheader("Content-Length", str(Content_Length))
self.conn.endheaders()
self.conn.send(data)
res = self.conn.getresponse()
if res.read() != "9898":
return False
userinfo = re.findall(r"aspsky.*?(?=;\s)", res.getheader("set-cookie"))
self.cookie = "BoardList=BoardID=Show; upNum=0; " + session + "; " + userinfo[0]
return True
def getStatus(self):
return self.status
def getCookie(self):
return self.cookie 展开
3个回答
展开全部
不知道你是学java的还是学Python的;
如果是想要Java的模拟登录代码,Java的代码很多,不需要拿Python的一段代码来模拟,(虽python 和 Java 都是面向对象编程语言,有很多共同点)
如果是想看看模仿这段python 代码做Java实现或者是“做互译训练”,其实上面的代码也不难:
class loginInfo(QtCore.QThread):
先是写了一个类loginInfo,继承QtCore.QThread类
def __init__(self, conn, parent):
初始化方法,默认的self参数(Java中不用理会,相当于this)
def run(self):
相当于Java中thread 中的 run() 方法,开启线程
def getConnection(self):、
做连接
def getStatus(self):
拿到状态
def getCookie(self):
和cookie 相关
如果是想要Java的模拟登录代码,Java的代码很多,不需要拿Python的一段代码来模拟,(虽python 和 Java 都是面向对象编程语言,有很多共同点)
如果是想看看模仿这段python 代码做Java实现或者是“做互译训练”,其实上面的代码也不难:
class loginInfo(QtCore.QThread):
先是写了一个类loginInfo,继承QtCore.QThread类
def __init__(self, conn, parent):
初始化方法,默认的self参数(Java中不用理会,相当于this)
def run(self):
相当于Java中thread 中的 run() 方法,开启线程
def getConnection(self):、
做连接
def getStatus(self):
拿到状态
def getCookie(self):
和cookie 相关
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询