Python WinError 10049:请求的地址在其上下文中无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23857942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WinError 10049: The requested address is not valid in its context
提问by user3451109
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:
我正在尝试在 Python 中发出原始 HTTP 请求并将响应写入文件。当我尝试绑定到已解析的 IP 地址或主机域时,我得到以下信息:
Traceback (most recent call last):
File "thingy.py", line 3, in <module> soc.bind(('168.62.48.183', 80))
OSError: [WinError 10049] The requested address is not valid in its context
回溯(最近一次调用最后一次):
File "thingy.py", line 3, in <module> soc.bind(('168.62.48.183', 80))
OSError: [WinError 10049] 请求的地址在其上下文中无效
I found a StackOverflow questionthat had the identical error, but it did not answer my question because it was for a listeningsocket. Here is my code:
我发现了一个StackOverflow 问题,它有相同的错误,但它没有回答我的问题,因为它是针对侦听套接字的。这是我的代码:
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()
回答by Torxed
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)
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。
您的机器不知道此 IP,因此会出现错误消息,如果您将其更改为说它127.0.0.1
可以工作,但是您再次需要一个.listen(4)
andns, na = soc.accept()
在 utelizing 之前.send()
,您soc.recv()
需要是ns.recv(1024)
.
In other words, you mixed up client sockets with server sockets and you're bindingto a IP not present on the local machine.
换句话说,您将客户端套接字与服务器套接字混在一起,并且绑定到本地计算机上不存在的 IP。
Also note: soc.recv()
will fail, you need a buffer-size argument like so: soc.recv(1024)
另请注意:soc.recv()
会失败,您需要一个缓冲区大小参数,如下所示:soc.recv(1024)
Python3:
蟒蛇3:
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)
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..
有两个主要区别,我们发送二进制GET /miners/..
字符串而不是标准字符串。其次,我们以二进制形式打开输出文件,因为接收到的数据也将是二进制形式。
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.
这是因为 Python 由于多种原因不再为您解码字符串,因此您需要将数据视为二进制数据或在此过程中手动对其进行解码。
You should probably:
你应该:
import urllib.request
f = urllib.request.urlopen("http://www.multiminerapp.com/miners/get?file=BFGMiner-3.99-r.1-win32.zip")
print(f.read())
回答by White Revolver
First, you must connect both devices to the same network. Then, for the server.py (or anything you want to call it)
首先,您必须将两个设备连接到同一网络。然后,对于 server.py (或任何你想调用它的东西)
Use
用
soc.bind(('', PORT))
Instead of
代替
soc.bind(('IP', PORT))
回答by Dipanshu Mahla
Create a server using third party softwares like xamp, wamp
使用 xamp、wamp 等第三方软件创建服务器
then,
然后,
soc.bind(('server_ip',port_number))