使用 pygame 在 python 中通过套接字流式传输 [英] using pygame to stream over sockets in python

查看:47
本文介绍了使用 pygame 在 python 中通过套接字流式传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个网络摄像头脚本,我在 python 中从互联网上获得,我正在使用 pygame 模块,问题是我的网络摄像头将打开,然后连接断开并说套接字正在使用中,服务器代码是

I am working on with a webcam script I got of the internet in python and I am using pygame module the issue is that my webcam wil open and then the connection drops and says the socket is aready in use the server code is

import socket

import pygame

import sys


port=5014


#create pygame screen

screen = pygame.display.set_mode((800,600),0)


while True:

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind(("",port)) # server is available on the whole network by setting host to ""

s.listen(1)

connection, addr = s.accept()

received = []


# loop .recv, it returns empty string when done, then transmitted data is completely received

while True:

    recvd_data = connection.recv(1440021)

    if not recvd_data:

        break

    else:

        received.append(recvd_data)



dataset = ''.join(received)

image = pygame.image.fromstring(dataset,(800,600),"RGB") # convert received image from string

#image = pygame.transform.scale(image,(800,600)) # scale image to 800*600

screen.blit(image,(0,0)) # "show image" on the screen

pygame.display.update()


# check for quit events

for event in pygame.event.get():

    if event.type == pygame.QUIT:

        pygame.quit()

        sys.exit()

客户端代码是

    import socket

    import pygame

    import pygame.camera

    import sys

    import time



   host = "localhost"

   port = 5014



   pygame.init()

   pygame.camera.init()


    cam_list = pygame.camera.list_cameras() # list available cameras

    webcam = pygame.camera.Camera(cam_list[0],(800,600)) # use first camera in list and set resolution

    webcam.start() # start camera


    while True:

    image = webcam.get_image() # capture image

    data = pygame.image.tostring(image,"RGB") # convert captured image to string, use RGB color scheme

    #print sys.getsizeof(data) # in case somebody wants to know the size of the captured   image


    # prepare for connection to server

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP is used

    s.connect((host, port))

    s.sendall(data)

    s.close()

    time.sleep(0.1)

我在服务器上得到的错误是

The error I get on the server is

Traceback (most recent call last):
  File "/root/Desktop/s.py", line 20, in <module>
    s.bind(("",port)) # server is available on the whole network by setting host to ""
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 98] Address already in us

我在客户端上得到的错误是

And the error I get on the client is

Traceback (most recent call last):
  File "/root/Desktop/c.py", line 45, in <module>
    s.connect((host, port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

是的,我已经更改了端口号,有人知道出了什么问题吗?

And yes I have changed the port numbers does anyone know what is wrong?

推荐答案

问题是在你的第一个无限循环中,你正在调用 s.bind().第一次可以用,后来地址已经在用了,绑定不了.

The problem is that inside your first infinite loop, you are calling s.bind(). It works the first time but after that, the address is already in use and can't bind.

s.bind() 和 socket.socket 不应该这样循环,它们只需要执行一次.然后使用无限循环来接受连接.

s.bind() and socket.socket should not be in a loop like that, they only need to be done once. Then use the infinite loop to accept connections.

这篇关于使用 pygame 在 python 中通过套接字流式传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆