Send the file and the filename using PythonHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?How do I copy a file in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I list all files of a directory?Does Python have a string 'contains' substring method?

Should I tell management that I intend to leave due to bad software development practices?

Blender 2.8 I can't see vertices, edges or faces in edit mode

Why are electrically insulating heatsinks so rare? Is it just cost?

Is there a hemisphere-neutral way of specifying a season?

What killed these X2 caps?

Is the Joker left-handed?

How to say in German "enjoying home comforts"

Alternative to sending password over mail?

How to draw the figure with four pentagons?

What exploit are these user agents trying to use?

What does it mean to describe someone as a butt steak?

What is the intuition behind short exact sequences of groups; in particular, what is the intuition behind group extensions?

Intersection of two sorted vectors in C++

UK: Is there precedent for the governments e-petition site changing the direction of a government decision?

Do I have a twin with permutated remainders?

How can saying a song's name be a copyright violation?

Why is consensus so controversial in Britain?

Watching something be written to a file live with tail

Why does Kotter return in Welcome Back Kotter

Can I ask the recruiters in my resume to put the reason why I am rejected?

Why do I get two different answers for this counting problem?

How do conventional missiles fly?

What is the PIE reconstruction for word-initial alpha with rough breathing?

Has there ever been an airliner design involving reducing generator load by installing solar panels?



Send the file and the filename using Python


How do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?How do I copy a file in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I list all files of a directory?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I want to make a dead simple server that accepts different types of files from clients. The problem is, that I cannot get the filename sent over from the client, so all the files are saved as 1 filename, whatever the file is called.



This is my client.py:



import socket
with socket.socket() as s:
s.connect(('192.168.1.2',10000))
with open('Image.jpg','rb') as f:
s.sendall(f.read())


And this is my server.py:



import socket
print("Server started")
while True:
with socket.socket() as s:
s.bind(('192.168.1.2',10000))
s.listen(1)
with s.accept()[0] as c:
chunks = []
while True:
chunk = c.recv(4096)
if not chunk: break
chunks.append(chunk)
print("Receiving file")
with open('Image.jpg','wb') as f:
f.write(b''.join(chunks))
print("File Received")


Which works fine. The problem starts, that if I send another file, for example "data.csv" it will still be saved as "Image.jpg" on the server.



Is there any way to also save the filename along with the file?










share|improve this question
























  • Maybe you could include it as a header, or as a message before the picture?

    – Reedinationer
    Mar 8 at 0:04











  • Sending files in clear text isn't a great idea nowadays, have you considered using scp ?

    – Benoît Pilatte
    Mar 8 at 0:13











  • You'll need to create a wire protocol that lets you include the file name (or use one that already can do so, like e.g. ftp...)

    – thebjorn
    Mar 8 at 0:22

















3















I want to make a dead simple server that accepts different types of files from clients. The problem is, that I cannot get the filename sent over from the client, so all the files are saved as 1 filename, whatever the file is called.



This is my client.py:



import socket
with socket.socket() as s:
s.connect(('192.168.1.2',10000))
with open('Image.jpg','rb') as f:
s.sendall(f.read())


And this is my server.py:



import socket
print("Server started")
while True:
with socket.socket() as s:
s.bind(('192.168.1.2',10000))
s.listen(1)
with s.accept()[0] as c:
chunks = []
while True:
chunk = c.recv(4096)
if not chunk: break
chunks.append(chunk)
print("Receiving file")
with open('Image.jpg','wb') as f:
f.write(b''.join(chunks))
print("File Received")


Which works fine. The problem starts, that if I send another file, for example "data.csv" it will still be saved as "Image.jpg" on the server.



Is there any way to also save the filename along with the file?










share|improve this question
























  • Maybe you could include it as a header, or as a message before the picture?

    – Reedinationer
    Mar 8 at 0:04











  • Sending files in clear text isn't a great idea nowadays, have you considered using scp ?

    – Benoît Pilatte
    Mar 8 at 0:13











  • You'll need to create a wire protocol that lets you include the file name (or use one that already can do so, like e.g. ftp...)

    – thebjorn
    Mar 8 at 0:22













3












3








3


1






I want to make a dead simple server that accepts different types of files from clients. The problem is, that I cannot get the filename sent over from the client, so all the files are saved as 1 filename, whatever the file is called.



This is my client.py:



import socket
with socket.socket() as s:
s.connect(('192.168.1.2',10000))
with open('Image.jpg','rb') as f:
s.sendall(f.read())


And this is my server.py:



import socket
print("Server started")
while True:
with socket.socket() as s:
s.bind(('192.168.1.2',10000))
s.listen(1)
with s.accept()[0] as c:
chunks = []
while True:
chunk = c.recv(4096)
if not chunk: break
chunks.append(chunk)
print("Receiving file")
with open('Image.jpg','wb') as f:
f.write(b''.join(chunks))
print("File Received")


Which works fine. The problem starts, that if I send another file, for example "data.csv" it will still be saved as "Image.jpg" on the server.



Is there any way to also save the filename along with the file?










share|improve this question
















I want to make a dead simple server that accepts different types of files from clients. The problem is, that I cannot get the filename sent over from the client, so all the files are saved as 1 filename, whatever the file is called.



This is my client.py:



import socket
with socket.socket() as s:
s.connect(('192.168.1.2',10000))
with open('Image.jpg','rb') as f:
s.sendall(f.read())


And this is my server.py:



import socket
print("Server started")
while True:
with socket.socket() as s:
s.bind(('192.168.1.2',10000))
s.listen(1)
with s.accept()[0] as c:
chunks = []
while True:
chunk = c.recv(4096)
if not chunk: break
chunks.append(chunk)
print("Receiving file")
with open('Image.jpg','wb') as f:
f.write(b''.join(chunks))
print("File Received")


Which works fine. The problem starts, that if I send another file, for example "data.csv" it will still be saved as "Image.jpg" on the server.



Is there any way to also save the filename along with the file?







python sockets






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 0:07









orde

4,81762533




4,81762533










asked Mar 8 at 0:02









Ioannis Tzon TsolakidisIoannis Tzon Tsolakidis

161




161












  • Maybe you could include it as a header, or as a message before the picture?

    – Reedinationer
    Mar 8 at 0:04











  • Sending files in clear text isn't a great idea nowadays, have you considered using scp ?

    – Benoît Pilatte
    Mar 8 at 0:13











  • You'll need to create a wire protocol that lets you include the file name (or use one that already can do so, like e.g. ftp...)

    – thebjorn
    Mar 8 at 0:22

















  • Maybe you could include it as a header, or as a message before the picture?

    – Reedinationer
    Mar 8 at 0:04











  • Sending files in clear text isn't a great idea nowadays, have you considered using scp ?

    – Benoît Pilatte
    Mar 8 at 0:13











  • You'll need to create a wire protocol that lets you include the file name (or use one that already can do so, like e.g. ftp...)

    – thebjorn
    Mar 8 at 0:22
















Maybe you could include it as a header, or as a message before the picture?

– Reedinationer
Mar 8 at 0:04





Maybe you could include it as a header, or as a message before the picture?

– Reedinationer
Mar 8 at 0:04













Sending files in clear text isn't a great idea nowadays, have you considered using scp ?

– Benoît Pilatte
Mar 8 at 0:13





Sending files in clear text isn't a great idea nowadays, have you considered using scp ?

– Benoît Pilatte
Mar 8 at 0:13













You'll need to create a wire protocol that lets you include the file name (or use one that already can do so, like e.g. ftp...)

– thebjorn
Mar 8 at 0:22





You'll need to create a wire protocol that lets you include the file name (or use one that already can do so, like e.g. ftp...)

– thebjorn
Mar 8 at 0:22












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054771%2fsend-the-file-and-the-filename-using-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054771%2fsend-the-file-and-the-filename-using-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved