我想创建一个程序来访问文件中的图像,对它们进行编码,然后将它们发送到服务器.比服务器应该解码图像,并将其保存到文件.我测试了图像编码本身,它工作,所以问题在于服务器和客户端连接.
这是服务器:
import socket
import errno
import base64
from PIL import Image
import StringIO
def connect(c):
try:
image = c.recv(8192)
return image
except IOError as e:
if e.errno == errno.EWOULDBLOCK:
connect(c)
def Main():
host = '138.106.180.21'
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host, port))
s.listen(1)
while True:
c, addr = s.accept()
c.setblocking(0)
print "Connection from: " + str(addr)
image = c.recv(8192)#connect(c)
imgname = 'test.png'
fh = open(imgname, "wb")
if image == 'cusdom_image':
with open('images.png', "rb") as imageFile: …Run Code Online (Sandbox Code Playgroud) 我想用Python制作服务器和客户端.它应该建立多个连接,一个是服务器向客户端发送内容,另一个是客户端向服务器发送内容的连接.
第一个连接工作正常,但第二个连接崩溃了消息:
socket.error: [Errno 9] Bad file descriptor
Run Code Online (Sandbox Code Playgroud)
这是服务器:
import socket
import errno
import pickle
def Main():
host = '188.174.233.99'
port = 66666
all_text = ['text1', 'text2', 'text3']
all_description = ['Test \n Description1\n', 'Test \n Description1\n', 'Test \n Description1\n']
all_images = ['unlock.png', 'unlock.png', 'unlock.png']
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
s.bind((host, port))
s.listen(1)
while True:
c, addr = s.accept()
c.setblocking(0)
print "Connection from: " + str(addr)
command = c.recv(1024)
if command == 'GIVEALL':
textstring = pickle.dumps([all_text, all_images, all_description])#verwandelt Liste in String …Run Code Online (Sandbox Code Playgroud)