首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTTP POSTusing套接字- python

HTTP POSTusing套接字- python
EN

Stack Overflow用户
提问于 2018-07-16 06:45:11
回答 1查看 42关注 0票数 0

我使用python中的套接字发出了一个POST请求,它工作得很好,但由于尝试迭代用户名和密码并不起作用,我已经尝试了几个小时,如果不是一整天的话,试图找出问题所在。

...i根本不明白这一点,代码将在下面(我已经尝试了许多不同的版本)。谢谢,真的很想知道我是不是出错了,我不认为我离得太远了?

代码语言:javascript
复制
#!/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迭代都有这个代码)

代码语言:javascript
复制
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太长的变量,但我现在得到另一个错误

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-16 06:54:13

内容长度不正确。你的帖子正文比你声称的要长:

代码语言:javascript
复制
length = len(str(user)) + len(str(pwd[x]))

这只计算用户和密码字符串的长度,但是您的内容主体仍然包含更多字符:

代码语言:javascript
复制
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并让它来完成所有繁重的工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51352753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档