WinError 10049:请求的地址在其上下文中无效

WinError 10049:请求的地址在其上下文中无效

问题描述:

我正在尝试使用Python发出原始HTTP请求,并将响应写入文件.当我尝试绑定到主机的解析IP地址或域时,得到以下信息:

I am trying to make a raw HTTP request in Python and write the response to a file. When I try to bind to the resolved IP Address or domain of the host I get this:

回溯(最近通话最近一次):

Traceback (most recent call last):

File "thingy.py", line 3, in <module>

  soc.bind(('168.62.48.183', 80))

OSError:[WinError 10049]请求的地址在其上下文中无效

OSError: [WinError 10049] The requested address is not valid in its context

我发现了一个 *问题相同的错误,但是它没有回答我的问题,因为它是针对 listening 套接字的.这是我的代码:

I found a * question that had the identical error, but it did not answer my question because it was for a listening socket. Here is my code:

from socket import *
soc = socket(AF_INET, SOCK_STREAM)
soc.bind(('168.62.48.183', 80))
soc.send('GET /miners/get?file=BFGMiner-3.99-r.1-win32.zip HTTP/1.1\nUser-Agent:MultiMiner/V3\nHost: www.multiminerapp.com\n')
response = soc.recv()
respfile = open("http-response.txt","w")
respfile.writelines(response)
respfile.close()

from socket import *
soc = socket(AF_INET, SOCK_STREAM)
soc.connect(('168.62.48.183', 80))
soc.send('GET /miners/get?file=BFGMiner-3.99-r.1-win32.zip HTTP/1.1\nUser-Agent:MultiMiner/V3\nHost: www.multiminerapp.com\n')
with open("http-response.txt","w") as respfile:
    response = soc.recv(1024) # <--- Use select.epoll or asyncore instead!
    respfile.writelines(response)

您的代码之所以失败的原因是因为您试图绑定到外部IP.
您的机器不知道该IP,因此会出现错误消息,如果您将其更改为127.0.0.1则可以使用,但是再次需要在将.send()soc.recv()必须为ns.recv(1024).

The reason for why your code fails tho is because you're trying to bind to an external IP.
Your machine is not aware of this IP hence the error message, if you'd change it to say 127.0.0.1 it would work, but then again you would need a .listen(4) and ns, na = soc.accept() before utelizing .send() and your soc.recv() would need to be ns.recv(1024).

换句话说,您将客户端套接字与服务器套接字混合在一起,并且绑定到本地计算机上不存在的IP.

In other words, you mixed up client sockets with server sockets and you're binding to a IP not present on the local machine.

也请注意:soc.recv()将失败,您需要像这样的缓冲区大小参数:soc.recv(1024)

Also note: soc.recv() will fail, you need a buffer-size argument like so: soc.recv(1024)

from socket import *
soc = socket(AF_INET, SOCK_STREAM)
soc.connect(('168.62.48.183', 80))
soc.send(b'GET /miners/get?file=BFGMiner-3.99-r.1-win32.zip HTTP/1.1\nUser-Agent:MultiMiner/V3\nHost: www.multiminerapp.com\n\n') # Note the double \n\n at the end.
with open("http-response.txt","wb") as respfile:
    response = soc.recv(8192)
    respfile.write(response)

有两个主要区别,我们发送二进制GET /miners/..字符串而不是标准字符串. 其次,我们以二进制形式打开输出文件,因为接收到的数据也将以二进制形式出现.

There's two major differences, we send a binary GET /miners/.. string rather than a standard string. Secondly we open the output-file in a binary form because the data recieved will also be in binary form..

这是因为由于多种原因,Python不再为您解码字符串,因此您需要将数据视为二进制数据,或者在此过程中手动对其进行解码.

This is because Python no longer decodes the string for you because of a number of reasons, so you need to either treat the data as binary or manually decode it along the way.

import urllib.request
f = urllib.request.urlopen("http://www.multiminerapp.com/miners/get?file=BFGMiner-3.99-r.1-win32.zip")
print(f.read())