OSError:[WinError 10049]如何解决此错误?

时间:2017-09-04 18:31:10

标签: python sockets video camera stream

我正在尝试使用python将视频源从Raspberry Pi传输到我的机器。因此,我需要将套接字连接到服务器。 (在执行这些之前,从RPi直接在192.168.0.6:8081流式传输视频)

在我的电脑上执行此代码:

import numpy as np
import cv2
import socket


class VideoStreamingTest(object):

    def __init__(self):
        self.server_socket = socket.socket()
        self.server_socket.bind(('192.168.0.6', 8081))
        self.server_socket.listen(0)
        self.connection, self.client_address = self.server_socket.accept()
        self.connection = self.connection.makefile('rb')
        self.streaming()

def streaming(self):
    try:
        print ("Connection from: ", self.client_address)
        print ("Streaming...")
        print ("Press 'q' to exit")

        stream_bytes = ' '
        while True:
            stream_bytes += self.connection.read(1024)
            first = stream_bytes.find('\xff\xd8')
            last = stream_bytes.find('\xff\xd9')
            if first != -1 and last != -1:
                jpg = stream_bytes[first:last + 2]
                stream_bytes = stream_bytes[last + 2:]
                #image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE)
                image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
                cv2.imshow('image', image)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
    finally:
        self.connection.close()
        self.server_socket.close()

if __name__ == '__main__':
    VideoStreamingTest()

在编译期间会引发此错误:

Traceback (most recent call last):

File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 43, in <module>
    VideoStreamingTest()

File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 11, in __init__
    self.server_socket.bind(('192.168.0.6', 8081))

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

编辑:这也是

Traceback (most recent call last):
  File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 47, in 
<module>
    VideoStreamingTest()
  File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 17, in 
__init__
self.connection = self.socket.create_connection(('192.168.0.6', 8081))
AttributeError: 'socket' object has no attribute 'create_connection'

所以我尝试用connect_ex替换create_connection,它仍然给出错误.. :(

class VideoStreamingTest(object):
    def __init__(self):

    #self.server_socket = socket.socket()
    #self.server_socket.bind(('192.168.0.6', 8081))
    #self.server_socket.listen(0)

    #self.connection, self.client_address = self.server_socket.accept()
    #self.connection = self.connection.makefile('rb')
    #self.streaming()
    self.socket = socket.socket()
    self.connection = self.socket.create_connection(('192.168.0.6', 8081))
    #self.socket.connect(('192.168.0.6', 8081))
    self.streaming()

def streaming(self):

    try:
        #print ("Connection from: ", self.client_address)
        print ("Streaming...")
        print ("Press 'q' to exit")

        stream_bytes = ' '
        while True:
            stream_bytes += self.connection.read(1024)
            first = stream_bytes.find('\xff\xd8')
            last = stream_bytes.find('\xff\xd9')
            if first != -1 and last != -1:
                jpg = stream_bytes[first:last + 2]
                stream_bytes = stream_bytes[last + 2:]
                #image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE)
                image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
                cv2.imshow('image', image)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
    finally:
        self.connection.close()
        self.socket.close()

if __name__ == '__main__':
    VideoStreamingTest()

1 个答案:

答案 0 :(得分:1)

您必须将套接字的bind命令中的IP地址更改为提供服务器的设备的IP地址,即您自己的PC的地址。您在此处发布的代码不会执行您想要执行的操作:它将创建新的server,并且不会充当可以连接到服务器的客户端!

这一行

self.server_socket.bind(('192.168.0.6', 8081))
self.server_socket.listen(0)

必须替换为

self.socket.connect(('192.168.0.6', 8081))

编辑:也许最好使用create_connection函数,因为它在更高的API级别上运行。所以你最终会得到这个初始化方法:

def __init__(self):
    self.socket = socket.socket()
    self.connection, _ = self.socket.create_connection(('192.168.0.6', 8081))
    self.streaming()

streaming方法中,您还必须删除此行:

print ("Connection from: ", self.client_address)