简体   繁体   English

发生Python套接字错误

[英]Python socket error occured

I wrote this code. 我写了这段代码。

import socket

host = 'localhost'
port = 3794
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host, port))
while 1:
        print 'Type message you want to send...'
        msg = raw_input()
        if msg == '':
                s.close()
                break
    s.sendall(msg)

and next execute this code. 然后执行此代码。

Traceback (most recent call last):
  File "socket.py", line 11, in ?
    s.bind((host, port))
  File "<string>", line 1, in bind
socket.error: (99, 'Cannot assign requested address')

What's wrong? 怎么了?

Do you know solutions? 你知道解决方案吗?

This means that you already have a socket bound to 3794 port. 这意味着您已经有一个绑定到3794端口的套接字。

It may be another application or it means that port didn't got released yet after the previous run of your own script (it happens, if script terminated improperly). 它可能是另一个应用程序,或者它意味着在您自己的脚本上一次运行之后端口尚未发布(如果脚本未正确终止,则会发生这种情况)。

Simply try to use another port number - I believe everything will work fine. 只需尝试使用另一个端口号 - 我相信一切都会正常工作。

I had this same problem and it was caused by trying to listen on the wrong host. 我有同样的问题,这是因为试图在错误的主机上听。 When I changed it to an IP that was actually associated with the machine the code was running on (localhost), the problem went away. 当我将其更改为与代码运行的机器(localhost)实际关联的IP时,问题就消失了。

This error comes up mostly due to the the port being already used by another application/service . 出现此错误主要是由于该端口已被其他应用程序/服务使用。 Choose a port number above the range of registered ports , ie 49151 选择高于注册端口范围的端口号,即49151

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM