无效模式(' wb')或文件名:' torecv.jpg'蟒蛇

时间:2016-11-25 07:02:09

标签: python file sockets runtime-error

我正在编写客户端 - 服务器实时屏幕截图流。我的程序工作几秒钟。我可以看到来自我的客户端的实时流,但是我的程序因此错误而崩溃:

Traceback (most recent call last): File "C:/Users/user-pc/PycharmProjects/TestsFor/Functions‌​/screenshotLive2/ser‌​ver files.py", line 51, in <module> receive_file(filename) File "C:/Users/user-pc/PycharmProjects/TestsFor/Functions‌​/screenshotLive2/ser‌​ver files.py", line 36, in receive_file f = open(file_name, 'wb') IOError: [Errno 22] invalid mode ('wb') or filename: 'torecv.jpg'

这真的很奇怪,因为它能够在粉碎之前多次打开和关闭它。有谁理解为什么?

这是我的服务器:

import socket               # Import socket module
import cv2
import struct
try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # put socket outside of loop?
    sock.bind(("0.0.0.0", 8200))
    sock.listen(1)
    connect, address = sock.accept()
except socket.error:
    print "Socket Error"


def recv_msg():
    # Read message length and unpack it into an integer
    raw_msglen = recvall(4)
    if not raw_msglen:
        return None
    msglen = struct.unpack('L', raw_msglen)[0]
    # Read the message data
    return recvall(msglen)


def recvall(n):
    # Helper function to recv n bytes or return None if EOF is hit
    data = ''
    while len(data) < n:
        packet = connect.recv(n - len(data))
        if not packet:
            return None
        data += packet
    return data


def receive_file(file_name):
    f = open(file_name, 'wb')
    data = recv_msg()
    f.writelines(data)
    f.close()

while True:
    receive_file(r'torecv.jpg')
    image = cv2.imread(r'torecv.jpg',1)
    cv2.imshow("Live Screen", image)
    cv2.waitKey(1)

这是我的客户:

import ImageGrab
import StringIO
import socket
import struct
try:
    connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connect.connect(("localhost", 8200))


except socket.error:
    print "Error Socket"


def screen_shot():
    im = ImageGrab.grab()  # take screen shot
    saver = StringIO.StringIO()  # creating string as 'File'
    im.save(saver, "jpeg")  # save the file (not on hard drive)
    binary_string = saver.getvalue()
    saver.close()
    msg = struct.pack('L', len(binary_string)) + binary_string
    return msg


while True:
    connect.sendall(screen_shot())

0 个答案:

没有答案