Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Server code actively refuses to connect with Udacity's Self Driving Car Simulator (Port 4567) #131

Open
thetechdude124 opened this issue Jan 8, 2021 · 17 comments

Comments

@thetechdude124
Copy link

thetechdude124 commented Jan 8, 2021

When running the drive.py and simulator files, the connection is never established - it simply says "accepted", rather than actually connecting. When I looked at the output log for the simulator, here's what I found:

`|Fatal|WebSocket.acceptException|System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.

                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x00000] in <filename unknown>:0 `

This error happens every time I try and establish a connection. Here's the code on the server side (drive.py file):

import base64 #for lossless encoding transfer
from datetime import datetime #to set frame timestamp
import os #write + read files
import numpy as np
import shutil
import socketio #server
from flask import Flask #framework for web devices
from io import BytesIO #manipulate string and byte data in memory
import eventlet
import eventlet.wsgi 
import cv2

import tensorflow as tf
import keras
from keras.models import load_model
from PIL import Image

height = 320
width = 160     

def resize(image):
    return cv2.resize(image, (width, height), cv2.INTER_AREA)

#server init
sio = socketio.Server(always_connect = True )
#flask web app
application = Flask(__name__)

#init empty model and image array
net = None
image_array_before = None

#Speed limits
max_speed = 30
min_speed = 10

speed_limit = max_speed

#Server event handler
@sio.on('telemetry')
def telemetry(sid, data):

    if data:
        steering_angle = float(data["steering_angle"])
        throttle = float(data["throttle"])
        speed = float(data["speed"])    
        image = Image.open(BytesIO(base64.b64decode(data["image"])))
        
        #save frame
        timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
        image_filename = os.path.join(r'path', timestamp)
        image.save('{}.jpg'.format(image_filename))
        
        try:
            image = np.asarray(image)
            image = resize(image)
            image = np.array([image])

            steering_angle = float(net.predict(image))

            global speed_limit
            if speed > speed_limit:   
                speed_limit = min_speed
            else:
                speed_limit = max_speed
            throttle = (1.0 - steering_angle**2 - (speed/speed_limit)**2)

            print ('{} {} {}'.format(steering_angle, throttle, speed))
            send_control(steering_angle, throttle)

        except Exception as e:
            print (e)

    else:
        
        sio.emit('manual', data={}, skip_sid = True)

@sio.on('connect')
def connect(sid, environ):
    print("connect ", sid)
    send_control(0,0) 

def send_control(steering_angle, throttle):
    sio.emit(
        "steer",
        data = {
            "steering_angle": steering_angle.__str__(),
            "throttle": throttle.__str__()
        },
        skip_sid = True)

if __name__ == "__main__":
    net = load_model('path')
    application = socketio.Middleware(sio, application)
    #deploy
    eventlet.wsgi.server(eventlet.listen(('localhost', 4567)), application)

I've tried to fix it via disabling the firewall and changing the hostname, but to no avail. Any idea what might be wrong? Thanks!

@techwithanirudh
Copy link

See this url about cv2 and ml https://youtu.be/mVUrErF5xq8

@techwithanirudh
Copy link

and try downgrading to python-socketio 4.2.1

@techwithanirudh
Copy link

and if it doesn't work try install version 2

@techwithanirudh
Copy link

of the Simulator

@haisenberg996
Copy link

并尝试降级到python-socketio 4.2.1
感谢感谢,成功解决了我的问题

@haisenberg996
Copy link

并尝试降级到python-socketio 4.2.1

很好奇,您是怎么想到这个解决方法的呢?

@thetechdude124
Copy link
Author

thetechdude124 commented Feb 4, 2021 via email

@Subhainr
Copy link

Subhainr commented Feb 5, 2021

When running the drive.py and simulator files, the connection is never established - it simply says "accepted", rather than actually connecting. When I looked at the output log for the simulator, here's what I found:

`|Fatal|WebSocket.acceptException|System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.

                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x00000] in <filename unknown>:0 `

This error happens every time I try and establish a connection. Here's the code on the server side (drive.py file):

import base64 #for lossless encoding transfer
from datetime import datetime #to set frame timestamp
import os #write + read files
import numpy as np
import shutil
import socketio #server
from flask import Flask #framework for web devices
from io import BytesIO #manipulate string and byte data in memory
import eventlet
import eventlet.wsgi 
import cv2

import tensorflow as tf
import keras
from keras.models import load_model
from PIL import Image

height = 320
width = 160     

def resize(image):
    return cv2.resize(image, (width, height), cv2.INTER_AREA)

#server init
sio = socketio.Server(always_connect = True )
#flask web app
application = Flask(__name__)

#init empty model and image array
net = None
image_array_before = None

#Speed limits
max_speed = 30
min_speed = 10

speed_limit = max_speed

#Server event handler
@sio.on('telemetry')
def telemetry(sid, data):

    if data:
        steering_angle = float(data["steering_angle"])
        throttle = float(data["throttle"])
        speed = float(data["speed"])    
        image = Image.open(BytesIO(base64.b64decode(data["image"])))
        
        #save frame
        timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
        image_filename = os.path.join(r'path', timestamp)
        image.save('{}.jpg'.format(image_filename))
        
        try:
            image = np.asarray(image)
            image = resize(image)
            image = np.array([image])

            steering_angle = float(net.predict(image))

            global speed_limit
            if speed > speed_limit:   
                speed_limit = min_speed
            else:
                speed_limit = max_speed
            throttle = (1.0 - steering_angle**2 - (speed/speed_limit)**2)

            print ('{} {} {}'.format(steering_angle, throttle, speed))
            send_control(steering_angle, throttle)

        except Exception as e:
            print (e)

    else:
        
        sio.emit('manual', data={}, skip_sid = True)

@sio.on('connect')
def connect(sid, environ):
    print("connect ", sid)
    send_control(0,0) 

def send_control(steering_angle, throttle):
    sio.emit(
        "steer",
        data = {
            "steering_angle": steering_angle.__str__(),
            "throttle": throttle.__str__()
        },
        skip_sid = True)

if __name__ == "__main__":
    net = load_model('path')
    application = socketio.Middleware(sio, application)
    #deploy
    eventlet.wsgi.server(eventlet.listen(('localhost', 4567)), application)

I've tried to fix it via disabling the firewall and changing the hostname, but to no avail. Any idea what might be wrong? Thanks!

pip install python-engineio==3.13.2
pip install python-socketio==4.6.1

let me know. do in your created environment only
Give a love by upvoting.

@sumit-mandal
Copy link

When running the drive.py and simulator files, the connection is never established - it simply says "accepted", rather than actually connecting. When I looked at the output log for the simulator, here's what I found:
`|Fatal|WebSocket.acceptException|System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.

                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x00000] in <filename unknown>:0 `

This error happens every time I try and establish a connection. Here's the code on the server side (drive.py file):

import base64 #for lossless encoding transfer
from datetime import datetime #to set frame timestamp
import os #write + read files
import numpy as np
import shutil
import socketio #server
from flask import Flask #framework for web devices
from io import BytesIO #manipulate string and byte data in memory
import eventlet
import eventlet.wsgi 
import cv2

import tensorflow as tf
import keras
from keras.models import load_model
from PIL import Image

height = 320
width = 160     

def resize(image):
    return cv2.resize(image, (width, height), cv2.INTER_AREA)

#server init
sio = socketio.Server(always_connect = True )
#flask web app
application = Flask(__name__)

#init empty model and image array
net = None
image_array_before = None

#Speed limits
max_speed = 30
min_speed = 10

speed_limit = max_speed

#Server event handler
@sio.on('telemetry')
def telemetry(sid, data):

    if data:
        steering_angle = float(data["steering_angle"])
        throttle = float(data["throttle"])
        speed = float(data["speed"])    
        image = Image.open(BytesIO(base64.b64decode(data["image"])))
        
        #save frame
        timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
        image_filename = os.path.join(r'path', timestamp)
        image.save('{}.jpg'.format(image_filename))
        
        try:
            image = np.asarray(image)
            image = resize(image)
            image = np.array([image])

            steering_angle = float(net.predict(image))

            global speed_limit
            if speed > speed_limit:   
                speed_limit = min_speed
            else:
                speed_limit = max_speed
            throttle = (1.0 - steering_angle**2 - (speed/speed_limit)**2)

            print ('{} {} {}'.format(steering_angle, throttle, speed))
            send_control(steering_angle, throttle)

        except Exception as e:
            print (e)

    else:
        
        sio.emit('manual', data={}, skip_sid = True)

@sio.on('connect')
def connect(sid, environ):
    print("connect ", sid)
    send_control(0,0) 

def send_control(steering_angle, throttle):
    sio.emit(
        "steer",
        data = {
            "steering_angle": steering_angle.__str__(),
            "throttle": throttle.__str__()
        },
        skip_sid = True)

if __name__ == "__main__":
    net = load_model('path')
    application = socketio.Middleware(sio, application)
    #deploy
    eventlet.wsgi.server(eventlet.listen(('localhost', 4567)), application)

I've tried to fix it via disabling the firewall and changing the hostname, but to no avail. Any idea what might be wrong? Thanks!

pip install python-engineio==3.13.2
pip install python-socketio==4.6.1

let me know. do in your created environment only

your method worked. I was stuck for almost 2 days. You are saviour brother

@mikel-brostrom
Copy link

Downgrading the libraries as explained here worked for me! Thank you so much!

@Subhainr
Copy link

When running the drive.py and simulator files, the connection is never established - it simply says "accepted", rather than actually connecting. When I looked at the output log for the simulator, here's what I found:
`|Fatal|WebSocket.acceptException|System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.

                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in <filename unknown>:0 
                    at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x00000] in <filename unknown>:0 `

This error happens every time I try and establish a connection. Here's the code on the server side (drive.py file):

import base64 #for lossless encoding transfer
from datetime import datetime #to set frame timestamp
import os #write + read files
import numpy as np
import shutil
import socketio #server
from flask import Flask #framework for web devices
from io import BytesIO #manipulate string and byte data in memory
import eventlet
import eventlet.wsgi 
import cv2

import tensorflow as tf
import keras
from keras.models import load_model
from PIL import Image

height = 320
width = 160     

def resize(image):
    return cv2.resize(image, (width, height), cv2.INTER_AREA)

#server init
sio = socketio.Server(always_connect = True )
#flask web app
application = Flask(__name__)

#init empty model and image array
net = None
image_array_before = None

#Speed limits
max_speed = 30
min_speed = 10

speed_limit = max_speed

#Server event handler
@sio.on('telemetry')
def telemetry(sid, data):

    if data:
        steering_angle = float(data["steering_angle"])
        throttle = float(data["throttle"])
        speed = float(data["speed"])    
        image = Image.open(BytesIO(base64.b64decode(data["image"])))
        
        #save frame
        timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
        image_filename = os.path.join(r'path', timestamp)
        image.save('{}.jpg'.format(image_filename))
        
        try:
            image = np.asarray(image)
            image = resize(image)
            image = np.array([image])

            steering_angle = float(net.predict(image))

            global speed_limit
            if speed > speed_limit:   
                speed_limit = min_speed
            else:
                speed_limit = max_speed
            throttle = (1.0 - steering_angle**2 - (speed/speed_limit)**2)

            print ('{} {} {}'.format(steering_angle, throttle, speed))
            send_control(steering_angle, throttle)

        except Exception as e:
            print (e)

    else:
        
        sio.emit('manual', data={}, skip_sid = True)

@sio.on('connect')
def connect(sid, environ):
    print("connect ", sid)
    send_control(0,0) 

def send_control(steering_angle, throttle):
    sio.emit(
        "steer",
        data = {
            "steering_angle": steering_angle.__str__(),
            "throttle": throttle.__str__()
        },
        skip_sid = True)

if __name__ == "__main__":
    net = load_model('path')
    application = socketio.Middleware(sio, application)
    #deploy
    eventlet.wsgi.server(eventlet.listen(('localhost', 4567)), application)

I've tried to fix it via disabling the firewall and changing the hostname, but to no avail. Any idea what might be wrong? Thanks!

pip install python-engineio==3.13.2
pip install python-socketio==4.6.1
let me know. do in your created environment only

your method worked. I was stuck for almost 2 days. You are saviour brother

Thank u vaiya, for ur kind words. Move on

@Subhainr
Copy link

Downgrading the libraries as explained here worked for me! Thank you so much!

U r Welcome!!

@AserElkhateeb99
Copy link

and try downgrading to python-socketio 4.2.1
how you did it?

@hpt1988
Copy link

hpt1988 commented Jul 11, 2021

pip3 install “python-socketio<4.3” “python-engineio<3.9 is the solution

@githubrandomuser2017
Copy link

When I looked at the output log for the simulator,

@thetechdude124 Where did you find the simulator's output log? I'm running the simulator on a Mac.

@HaoranZhuExplorer
Copy link

solved my problem. thanks!

1 similar comment
@tensorway
Copy link

solved my problem. thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests