+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Share files with python server

发布于2020-10-30 21:40     阅读(917)     评论(0)     点赞(7)     收藏(4)


0

1

2

3

4

5

6

I' m working on a python server. I' ve already created the server and the client too. Server :

import socket, threading

class ClientThread(threading.Thread):

def __init__(self,ip,port):
    threading.Thread.__init__(self)
    self.ip = ip
    self.port = port
    print "[+] New thread started for "+ip+":"+str(port)

def run(self):    
    print "Connection from : "+ip+":"+str(port)
    stop = False
    while stop == False:
        try:
             data = clientsock.recv(2048)
             print "["+ip+"] : "+data
             clientsock.send("You sent me: "+data)
        except:
             stop = True
    print "[-] Close thread for  "+ip

host = "x.x.x.x"
port = 9999

tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

tcpsock.bind((host,port))
threads = []


while True:
    tcpsock.listen(4)
    print "\nListening for incoming connections..."
    (clientsock, (ip, port)) = tcpsock.accept()
    newthread = ClientThread(ip, port)
    newthread.start()
    threads.append(newthread)

for t in threads:
    t.join()

Client:

import socket
import sys
HOST, PORT = "x.x.x.x", 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
print " Connect to server !!"
msg='o'
while msg != "close":
    msg= raw_input("Ask user for something >>")
    sock.sendall(msg)
    try:
        msg1 = sock.recv(2048)
    except:
        print"server closed"
        msg1='nothing !!'
        msg='close'
    print'server reply >' +msg1
    print""
print"just press enter"
raw_input()

Now I' m tring to share (between client and server) not only messages by string's variable but some files too. For example, I what to send from the client to the server a .jpg file. Any ideas or suggestions about how can i do it ?


解决方案


Thanks a lot Ajay, you found the way ! I didn' t see the post you' ve linked when I searched it ! I write down belove the post you indicated : Sending Txt file to server from client using python sockets

0

1

2

3

4

5



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/609627/faa3f072a20aea78b7a2/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

7 0
收藏该文
已收藏

评论内容:(最多支持255个字符)