我使用python中的套接字发出了一个POST请求,它工作得很好,但由于尝试迭代用户名和密码并不起作用,我已经尝试了几个小时,如果不是一整天的话,试图找出问题所在。
...i根本不明白这一点,代码将在下面(我已经尝试了许多不同的版本)。谢谢,真的很想知道我是不是出错了,我不认为我离得太远了?
#!/usr/bin/python
#-*- coding: utf8 -*-
#imports
import socket
users = ['bill','ted','sally','sue']
num_00_09 = ['00','01','02','03','04','05','06','07','08','09']
num_10_100 = [x for x in range(10,101)]
pwd = num_00_09 + num_10_100
for user in users:
for x in range(1,101):
length = len(str(user)) + len(str(pwd[x]))
req = '''POST /python/login2r.php HTTP/1.1
Host: ad.samsclass.info
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: %s''' % (length)
req1 = '''\nCookie: __cfduid=d97d8c22217a6727cbe1a7d222f5f27ec1531510998
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
u=%s&p=%s''' % (user,pwd[x])
s = socket.socket()
socket.setdefaulttimeout(2)
s.connect(('ad.samsclass.info',80))
s.send(bytes(req + req1,'utf8'))
r = s.recv(1024)
print(r.decode('utf8'))
s.close()
这是错误代码(很明显,每次post迭代都有这个代码)
HTTP/1.1 400 Bad Request
Date: Sun, 15 Jul 2018 22:41:36 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 313
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at attack.samsclass.info Port 80</address>
</body></html>
我添加了+5太长的变量,但我现在得到另一个错误
HTTP/1.1 400 Bad Request
Date: Sun, 15 Jul 2018 22:56:49 GMT
Server: Apache/2.4.18 (Ubuntu)
Content-Length: 313
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at attack.samsclass.info Port 80</address>
</body></html>
Traceback (most recent call last):
File "file02.py", line 42, in <module>
s.send(bytes(req + req1,'utf8'))
OSError: [WinError 10038] An operation was attempted on something that is not a socket
发布于 2018-07-16 06:54:13
内容长度不正确。你的帖子正文比你声称的要长:
length = len(str(user)) + len(str(pwd[x]))
这只计算用户和密码字符串的长度,但是您的内容主体仍然包含更多字符:
u=%s&p=%s
u=
和&p=
字符也是POST正文的一部分,因此您的内容长度至少比您告诉服务器预期的长度长5个字节。
接下来,您将发送包含大量前导空格的报头。req = '''...'''
和req1 = '''...'''
字符串上的缩进是字符串值的一部分,但是HTTP头在发送时不应该缩进。删除那个空格。
接下来,HTTP要求您在发送标头时在它们之间同时包含回车符和换行符。您只发送\n
分隔符,而不发送\r\n
分隔符。
您最好使用Python附带的http.client
library。至少,尝试study it's source code,这样您就可以了解该库做什么,如果您必须尝试使用套接字的困难方式。
如果你不需要这么低级,只需安装requests
并让它来完成所有繁重的工作。
https://stackoverflow.com/questions/51352753
复制相似问题