In python how to receive and save image in HTTP post request without using requests and other libraries that are not installed in python
I try to receive image from HTTP post request in python. I am using BaseHTTPHolder and do_POST() function. I try to receive image from localhost address http:127.0.0.1:8080/photo while running server on localhost. I tried to use code below, it saves photo but doesn't allow to open because photo is not received fully.
img = urllib2.urlopen('http://127.0.0.1:8080/photo').read()
content_length = int(self.headers.getheader('content-length',0))
file_content = self.rfile.read(content_length)
with open('/Users/kasymhan/Desktop/sprint2/file01.jpg','wb') as s:
s.write(file_content)
EDIT
My do_POST() function
def do_POST(self):
url = 'http://127.0.0.1:8080/photo/file02.jpg'
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
file_content = self.rfile.read(length)
img = urllib2.urlopen(url).read()
with open('/Users/kasymhan/Desktop/sprint2/image.jpg','wb') as s:
s.write(img)
python http post urllib2 urllib
add a comment |
I try to receive image from HTTP post request in python. I am using BaseHTTPHolder and do_POST() function. I try to receive image from localhost address http:127.0.0.1:8080/photo while running server on localhost. I tried to use code below, it saves photo but doesn't allow to open because photo is not received fully.
img = urllib2.urlopen('http://127.0.0.1:8080/photo').read()
content_length = int(self.headers.getheader('content-length',0))
file_content = self.rfile.read(content_length)
with open('/Users/kasymhan/Desktop/sprint2/file01.jpg','wb') as s:
s.write(file_content)
EDIT
My do_POST() function
def do_POST(self):
url = 'http://127.0.0.1:8080/photo/file02.jpg'
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
file_content = self.rfile.read(length)
img = urllib2.urlopen(url).read()
with open('/Users/kasymhan/Desktop/sprint2/image.jpg','wb') as s:
s.write(img)
python http post urllib2 urllib
Please include a larger chunk of your code to see how you're using thedo_POST()
function.
– Edgar R. Mondragón
Nov 13 '18 at 19:59
add a comment |
I try to receive image from HTTP post request in python. I am using BaseHTTPHolder and do_POST() function. I try to receive image from localhost address http:127.0.0.1:8080/photo while running server on localhost. I tried to use code below, it saves photo but doesn't allow to open because photo is not received fully.
img = urllib2.urlopen('http://127.0.0.1:8080/photo').read()
content_length = int(self.headers.getheader('content-length',0))
file_content = self.rfile.read(content_length)
with open('/Users/kasymhan/Desktop/sprint2/file01.jpg','wb') as s:
s.write(file_content)
EDIT
My do_POST() function
def do_POST(self):
url = 'http://127.0.0.1:8080/photo/file02.jpg'
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
file_content = self.rfile.read(length)
img = urllib2.urlopen(url).read()
with open('/Users/kasymhan/Desktop/sprint2/image.jpg','wb') as s:
s.write(img)
python http post urllib2 urllib
I try to receive image from HTTP post request in python. I am using BaseHTTPHolder and do_POST() function. I try to receive image from localhost address http:127.0.0.1:8080/photo while running server on localhost. I tried to use code below, it saves photo but doesn't allow to open because photo is not received fully.
img = urllib2.urlopen('http://127.0.0.1:8080/photo').read()
content_length = int(self.headers.getheader('content-length',0))
file_content = self.rfile.read(content_length)
with open('/Users/kasymhan/Desktop/sprint2/file01.jpg','wb') as s:
s.write(file_content)
EDIT
My do_POST() function
def do_POST(self):
url = 'http://127.0.0.1:8080/photo/file02.jpg'
request_headers = self.headers
content_length = request_headers.getheaders('content-length')
length = int(content_length[0]) if content_length else 0
file_content = self.rfile.read(length)
img = urllib2.urlopen(url).read()
with open('/Users/kasymhan/Desktop/sprint2/image.jpg','wb') as s:
s.write(img)
python http post urllib2 urllib
python http post urllib2 urllib
edited Nov 13 '18 at 20:03
Kassymkhan Bekbolatov
asked Nov 13 '18 at 0:15
Kassymkhan BekbolatovKassymkhan Bekbolatov
13
13
Please include a larger chunk of your code to see how you're using thedo_POST()
function.
– Edgar R. Mondragón
Nov 13 '18 at 19:59
add a comment |
Please include a larger chunk of your code to see how you're using thedo_POST()
function.
– Edgar R. Mondragón
Nov 13 '18 at 19:59
Please include a larger chunk of your code to see how you're using the
do_POST()
function.– Edgar R. Mondragón
Nov 13 '18 at 19:59
Please include a larger chunk of your code to see how you're using the
do_POST()
function.– Edgar R. Mondragón
Nov 13 '18 at 19:59
add a comment |
1 Answer
1
active
oldest
votes
Why not directly write the contents of the image to the output file:
import urllib2
url = "http://farm5.static.flickr.com/4105/5063092779_17b2133825_b.jpg"
img = urllib2.urlopen(url).read()
with open('./image.jpg','wb') as s:
s.write(img)
I am sending image from computer to server that is running on 127.0.0.1. I am not sure how to specify file in '127.0.0.1:8080/photo' this url.
– Kassymkhan Bekbolatov
Nov 13 '18 at 19:55
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271974%2fin-python-how-to-receive-and-save-image-in-http-post-request-without-using-reque%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why not directly write the contents of the image to the output file:
import urllib2
url = "http://farm5.static.flickr.com/4105/5063092779_17b2133825_b.jpg"
img = urllib2.urlopen(url).read()
with open('./image.jpg','wb') as s:
s.write(img)
I am sending image from computer to server that is running on 127.0.0.1. I am not sure how to specify file in '127.0.0.1:8080/photo' this url.
– Kassymkhan Bekbolatov
Nov 13 '18 at 19:55
add a comment |
Why not directly write the contents of the image to the output file:
import urllib2
url = "http://farm5.static.flickr.com/4105/5063092779_17b2133825_b.jpg"
img = urllib2.urlopen(url).read()
with open('./image.jpg','wb') as s:
s.write(img)
I am sending image from computer to server that is running on 127.0.0.1. I am not sure how to specify file in '127.0.0.1:8080/photo' this url.
– Kassymkhan Bekbolatov
Nov 13 '18 at 19:55
add a comment |
Why not directly write the contents of the image to the output file:
import urllib2
url = "http://farm5.static.flickr.com/4105/5063092779_17b2133825_b.jpg"
img = urllib2.urlopen(url).read()
with open('./image.jpg','wb') as s:
s.write(img)
Why not directly write the contents of the image to the output file:
import urllib2
url = "http://farm5.static.flickr.com/4105/5063092779_17b2133825_b.jpg"
img = urllib2.urlopen(url).read()
with open('./image.jpg','wb') as s:
s.write(img)
answered Nov 13 '18 at 18:30
Edgar R. MondragónEdgar R. Mondragón
1,5112719
1,5112719
I am sending image from computer to server that is running on 127.0.0.1. I am not sure how to specify file in '127.0.0.1:8080/photo' this url.
– Kassymkhan Bekbolatov
Nov 13 '18 at 19:55
add a comment |
I am sending image from computer to server that is running on 127.0.0.1. I am not sure how to specify file in '127.0.0.1:8080/photo' this url.
– Kassymkhan Bekbolatov
Nov 13 '18 at 19:55
I am sending image from computer to server that is running on 127.0.0.1. I am not sure how to specify file in '127.0.0.1:8080/photo' this url.
– Kassymkhan Bekbolatov
Nov 13 '18 at 19:55
I am sending image from computer to server that is running on 127.0.0.1. I am not sure how to specify file in '127.0.0.1:8080/photo' this url.
– Kassymkhan Bekbolatov
Nov 13 '18 at 19:55
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271974%2fin-python-how-to-receive-and-save-image-in-http-post-request-without-using-reque%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please include a larger chunk of your code to see how you're using the
do_POST()
function.– Edgar R. Mondragón
Nov 13 '18 at 19:59