[英]using pygame to stream over sockets in python
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我正在使用我在 python 中从互联网上获得的网络摄像头脚本,并且我正在使用 pygame 模块,问题是我的网络摄像头将打开,然后连接断开并说套接字正在使用中,服务器代码是
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()
And the client code is客户端代码是
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?是的,我已经更改了端口号,有人知道出了什么问题吗?
In the server you should create the socket only once (outside while), then accept
multiple connections (inside while).在服务器中,您应该只创建一次套接字(在 while 之外),然后
accept
多个连接(在 while 内)。
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)
while True:
connection, addr = s.accept()
received = []
...
The problem is that inside your first infinite loop, you are calling s.bind().问题是在你的第一个无限循环中,你正在调用 s.bind()。 It works the first time but after that, the address is already in use and can't bind.
它第一次工作,但在那之后,地址已经在使用中,无法绑定。
s.bind() and socket.socket should not be in a loop like that, they only need to be done once. s.bind() 和 socket.socket 不应该这样循环,它们只需要执行一次。 Then use the infinite loop to accept connections.
然后使用无限循环来接受连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.