Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python socket error occured

Tags:

python

sockets

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?

like image 824
freddiefujiwara Avatar asked Dec 14 '22 02:12

freddiefujiwara


1 Answers

This means that you already have a socket bound to 3794 port.

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.

like image 61
Vanya Avatar answered Dec 24 '22 20:12

Vanya