Implementing CURL command using libcurl in C/C++

2019-12-13 Thread Karthik Sharma
The `CURL` command that I am using is shown below.

curl -F 'file=@/home/karthik/Workspace/downloadfile.out' 
http://127.0.0.1:5000/file-upload --verbose

The response from the server is shown below.

*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /file-upload HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 663876790
> Expect: 100-continue
> Content-Type: multipart/form-data; 
boundary=4e96ef0714498bd7
> 
< HTTP/1.1 100 Continue
* HTTP 1.0, assume close after body
< HTTP/1.0 201 CREATED
< Content-Type: application/json
< Content-Length: 46
< Server: Werkzeug/0.16.0 Python/3.5.2
< Date: Sat, 14 Dec 2019 07:05:15 GMT
< 
{
  "message": "File successfully uploaded"
}
* Closing connection 0

I want to implement the same command in C/C++ using libcurl. I am using the 
following function.

int FileUploadDownload::upload(const std::string &filename, const 
std::string &url) {

CURL *curl;
CURLcode res;
struct stat file_info;
curl_off_t speed_upload, total_time;
FILE *fd;

fd = fopen(filename.c_str(), "rb");
if(!fd) {
m_logger->errorf("unable to open file: %s\n",strerror(errno));
return 1;
}
if(fstat(fileno(fd), &file_info) != 0) {
m_logger->errorf("unable to get file stats: %s\n",strerror(errno));
return 2;
}

std::cout << "filename : "<< filename << std::endl;
std::cout << "url : " << url << std::endl;

curl = curl_easy_init();
if(curl) {

curl_easy_setopt(curl, CURLOPT_URL,
 url.c_str());

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, filename.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
 (curl_off_t) file_info.st_size);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

res = curl_easy_perform(curl);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_perform() failed: 
%s\n",curl_easy_strerror(res));
} else {
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
m_logger->infof("Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec 
during %"
CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",
speed_upload,
(total_time / 100), (long) (total_time % 
100));
}
}
return 0;
}
The below is the result that I get from the server.

The result that I get is shown below.
   *   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /file-upload HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 550
> Expect: 100-continue
> Content-Type: multipart/form-data; 
boundary=c8ef4837136fca99
> 
< HTTP/1.1 100 Continue
* HTTP 1.0, assume close after body
< HTTP/1.0 201 CREATED
< Content-Type: application/json
< Content-Length: 46
< Server: Werkzeug/0.16.0 Python/3.5.2
< Date: Sat, 14 Dec 2019 07:09:47 GMT
< 
{
  "message": "File successfully uploaded"
}
* Closing connection 0


My aim is to mimic the curl command above in the C/C++ code below. What am I 
doing wrong ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Implementing CURL command using libcurl in C/C++

2019-12-13 Thread Bob Gailer
This list is for Python, not C/C++.

On Dec 13, 2019 3:50 AM, "Karthik Sharma"  wrote:

> The `CURL` command that I am using is shown below.
>
> curl -F 'file=@/home/karthik/Workspace/downloadfile.out'
> http://127.0.0.1:5000/file-upload --verbose
>
> The response from the server is shown below.
>
> *   Trying 127.0.0.1...
> * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> > POST /file-upload HTTP/1.1
> > Host: 127.0.0.1:5000
> > User-Agent: curl/7.47.0
> > Accept: */*
> > Content-Length: 663876790
> > Expect: 100-continue
> > Content-Type: multipart/form-data; boundary=-
> ---4e96ef0714498bd7
> >
> < HTTP/1.1 100 Continue
> * HTTP 1.0, assume close after body
> < HTTP/1.0 201 CREATED
> < Content-Type: application/json
> < Content-Length: 46
> < Server: Werkzeug/0.16.0 Python/3.5.2
> < Date: Sat, 14 Dec 2019 07:05:15 GMT
> <
> {
>   "message": "File successfully uploaded"
> }
> * Closing connection 0
>
> I want to implement the same command in C/C++ using libcurl. I am using
> the following function.
>
> int FileUploadDownload::upload(const std::string &filename, const
> std::string &url) {
>
> CURL *curl;
> CURLcode res;
> struct stat file_info;
> curl_off_t speed_upload, total_time;
> FILE *fd;
>
> fd = fopen(filename.c_str(), "rb");
> if(!fd) {
> m_logger->errorf("unable to open file: %s\n",strerror(errno));
> return 1;
> }
> if(fstat(fileno(fd), &file_info) != 0) {
> m_logger->errorf("unable to get file stats:
> %s\n",strerror(errno));
> return 2;
> }
>
> std::cout << "filename : "<< filename << std::endl;
> std::cout << "url : " << url << std::endl;
>
> curl = curl_easy_init();
> if(curl) {
>
> curl_easy_setopt(curl, CURLOPT_URL,
>  url.c_str());
>
> curl_easy_setopt(curl, CURLOPT_POSTFIELDS, filename.c_str());
> curl_easy_setopt(curl, CURLOPT_POST, 1L);
> curl_easy_setopt(curl, CURLOPT_READDATA, fd);
> curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
>  (curl_off_t) file_info.st_size);
> curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
>
> res = curl_easy_perform(curl);
> if (res != CURLE_OK) {
> m_logger->errorf("curl_easy_perform() failed:
> %s\n",curl_easy_strerror(res));
> } else {
> curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD,
> &speed_upload);
> curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
> m_logger->infof("Speed: %" CURL_FORMAT_CURL_OFF_T "
> bytes/sec during %"
> CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",
> speed_upload,
> (total_time / 100), (long) (total_time
> % 100));
> }
> }
> return 0;
> }
> The below is the result that I get from the server.
>
> The result that I get is shown below.
>*   Trying 127.0.0.1...
> * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> > POST /file-upload HTTP/1.1
> > Host: 127.0.0.1:5000
> > User-Agent: curl/7.47.0
> > Accept: */*
> > Content-Length: 550
> > Expect: 100-continue
> > Content-Type: multipart/form-data; boundary=-
> ---c8ef4837136fca99
> >
> < HTTP/1.1 100 Continue
> * HTTP 1.0, assume close after body
> < HTTP/1.0 201 CREATED
> < Content-Type: application/json
> < Content-Length: 46
> < Server: Werkzeug/0.16.0 Python/3.5.2
> < Date: Sat, 14 Dec 2019 07:09:47 GMT
> <
> {
>   "message": "File successfully uploaded"
> }
> * Closing connection 0
>
>
> My aim is to mimic the curl command above in the C/C++ code below. What am
> I doing wrong ?
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-13 Thread Antoon Pardon
On 11/12/19 14:42, R.Wieser wrote:
> Michael,
>
>> It's a two-way street.  It's hard to teach someone that won't be taught.
> Whacking a student over the head because he doesn't understand has never 
> helped the student*.

You were not whacked for not understanding. You were wacked for
pretending to
understand better than the experts here. You were telling the experts
that what
they were telling you about python couldn't be true.

>So, stop whacking and start /teaching/.   Like 
> thinking of examples that could show the student where he goes wrong.

Stop arguing and start listening.

-- 
Antoon Pardon

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: More efficient/elegant branching

2019-12-13 Thread Antoon Pardon
On 9/12/19 12:27, Musbur wrote:
> Hello,
>
> I have a function with a long if/elif chain that sets a couple of
> variables according to a bunch of test expressions, similar to
> function branch1() below. I never liked that approach much because it
> is clumsy and repetetive, and pylint thinks so as well. I've come up
> with two alternatives which I believe are less efficient due to the
> reasons given in the respective docstrings. Does anybody have a better
> idea?
>
> def branch1(a, b, z):
>     """Inelegant, unwieldy, and pylint complains
>     about too many branches"""
>     if a > 4 and b == 0:
>     result = "first"
>     elif len(z) < 2:
>     result = "second"
>     elif b + a == 10:
>     result = "third"
>     return result
>
> def branch2(a, b, z):
>     """Elegant but inefficient because all expressions
>     are pre-computed althogh the first one is most likely
>     to hit"""
>     decision = [
>     (a > 4 and b == 0, "first"),
>     (len(z) < 2,   "second"),
>     (b + a == 10,  "third")]
>     for (test, result) in decision:
>     if test: return result
>
> def branch3(a, b, z):
>     """Elegant but inefficient because expressions
>     need to be parsed each time"""
>     decision = [
>     ("a > 4 and b == 0", "first"),
>     ("len(z) < 2",   "second"),
>     ("b + a == 10",  "third")]
>     for (test, result) in decision:
>     if eval(test): return result
> (env) [dh@deham01in015:~/python/rscl_fdc/devel]$
>
Well if you really want to go this route, you may consider the following:

def branch4(a, b, z):
decision = [
((lambda: a > 4 and b == 0), "first"),
((lambda: len(z) < 2), "second"),
((lambda: b + a == 10), "third")]
for test, result in decision:
if test(): return result


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Implementing CURL command using libcurl in C/C++

2019-12-13 Thread Barry


> On 13 Dec 2019, at 08:53, Karthik Sharma  wrote:
> 
> The `CURL` command that I am using is shown below.
> 
>curl -F 'file=@/home/karthik/Workspace/downloadfile.out' 
> http://127.0.0.1:5000/file-upload --verbose
> 
> The response from the server is shown below.
> 
>*   Trying 127.0.0.1...
>* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
>> POST /file-upload HTTP/1.1
>> Host: 127.0.0.1:5000
>> User-Agent: curl/7.47.0
>> Accept: */*
>> Content-Length: 663876790
>> Expect: 100-continue
>> Content-Type: multipart/form-data; 
>> boundary=4e96ef0714498bd7
>> 
>< HTTP/1.1 100 Continue
>* HTTP 1.0, assume close after body
>< HTTP/1.0 201 CREATED
>< Content-Type: application/json
>< Content-Length: 46
>< Server: Werkzeug/0.16.0 Python/3.5.2
>< Date: Sat, 14 Dec 2019 07:05:15 GMT
>< 
>{
>  "message": "File successfully uploaded"
>}
>* Closing connection 0
> 
> I want to implement the same command in C/C++ using libcurl. I am using the 
> following function.
> 
>int FileUploadDownload::upload(const std::string &filename, const 
> std::string &url) {
> 
>CURL *curl;
>CURLcode res;
>struct stat file_info;
>curl_off_t speed_upload, total_time;
>FILE *fd;
> 
>fd = fopen(filename.c_str(), "rb");
>if(!fd) {
>m_logger->errorf("unable to open file: %s\n",strerror(errno));
>return 1;
>}
>if(fstat(fileno(fd), &file_info) != 0) {
>m_logger->errorf("unable to get file stats: %s\n",strerror(errno));
>return 2;
>}
> 
>std::cout << "filename : "<< filename << std::endl;
>std::cout << "url : " << url << std::endl;
> 
>curl = curl_easy_init();
>if(curl) {
> 
>curl_easy_setopt(curl, CURLOPT_URL,
> url.c_str());
> 
>curl_easy_setopt(curl, CURLOPT_POSTFIELDS, filename.c_str());
>curl_easy_setopt(curl, CURLOPT_POST, 1L);
>curl_easy_setopt(curl, CURLOPT_READDATA, fd);
>curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
> (curl_off_t) file_info.st_size);
>curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
> 
>res = curl_easy_perform(curl);
>if (res != CURLE_OK) {
>m_logger->errorf("curl_easy_perform() failed: 
> %s\n",curl_easy_strerror(res));
>} else {
>curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
>curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
>m_logger->infof("Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec 
> during %"
>CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",
>speed_upload,
>(total_time / 100), (long) (total_time % 
> 100));
>}
>}
>return 0;
>}
> The below is the result that I get from the server.
> 
>The result that I get is shown below.
>   *   Trying 127.0.0.1...
>* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
>> POST /file-upload HTTP/1.1
>> Host: 127.0.0.1:5000
>> User-Agent: curl/7.47.0
>> Accept: */*
>> Content-Length: 550
>> Expect: 100-continue
>> Content-Type: multipart/form-data; 
>> boundary=c8ef4837136fca99
>> 
>< HTTP/1.1 100 Continue
>* HTTP 1.0, assume close after body
>< HTTP/1.0 201 CREATED
>< Content-Type: application/json
>< Content-Length: 46
>< Server: Werkzeug/0.16.0 Python/3.5.2
>< Date: Sat, 14 Dec 2019 07:09:47 GMT
>< 
>{
>  "message": "File successfully uploaded"
>}
>* Closing connection 0
> 
> 
> My aim is to mimic the curl command above in the C/C++ code below. What am I 
> doing wrong ?

My guess Is that there is way to handle the 100 continue and you are missing 
that code. Or you need to tell curl not to use the send the “Expect: 100 
Continue” header.

The purpose of the 100 Continue is to allow the server to refuse the POST.

https://tools.ietf.org/html/rfc7231#section-6.2.1

Barry

> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Troubleshooting

2019-12-13 Thread Akkana Peck
Michael Torrie writes:
> On 12/12/19 6:33 PM, Python wrote:
> > What happened exactly? Did you download the official installer from
> > python.org, then click on next, next, next, checking the box (if it's
> > still there about updating PATH)? There is nothing more to do.
> 
> I've seen github bug reports about Windows launching the store when
> trying to run Python, but those appear to be after a successful
> installation, and the solution there is making sure the installed
> version of Python is set in the $PATH.

I installed Python on a Windows 10 box for the first time a few weeks
ago (normally I'm a Linux user and get Python via distro packages).

Typing "python" at a command prompt before installing anything
gave a prompt for the MS store.

If you run an installer from Python.org, the checkbox to add to the
system path is not at all obvious. It's at the very bottom of a long
scrolled list of options, *and it's the only one not checked by
default*. The first time I installed, I looked at the list, scrolled
down a little way and concluded that all options were checked,
so I didn't scroll down to the bottom to notice the one crucial
un-checked one. (Go ahead and think poorly of me for making
assumptions, but I'm sure I'm not the only person to do that.)
And of course, the install didn't work and I had to reinstall.
Fortunately I know what a path is and knew to hunt for that the
second time through; I doubt a typical Windows person knows about paths.

It would make the install a lot easier for less technical Windows
people if the installer had a separate screen for the path option,
perhaps with an explanation of what path means (and that if you don't
understand the explanation, you really, really should check the box).
Or enable it by default -- would anyone besides an expert user
want it disabled? Or at least put it first in the long list of
options, so it always shows.

...Akkana
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reading mail getting [, ...

2019-12-13 Thread Abdur-Rahmaan Janhangeer
Can you please indicate where is the header in my code above? Thanks.

Yours,

Abdur-Rahmaan Janhangeer
pythonmembers.club  | github

Mauritius


>
-- 
https://mail.python.org/mailman/listinfo/python-list


3rd party mail package

2019-12-13 Thread Abdur-Rahmaan Janhangeer
Greetings everybody,

What is the best 3rd party package for reading mail? Got a  gmail 0.6.x on
pypi but it is highly inconsistent (and last commit 2013). Any
recommendations?

Yours,

Abdur-Rahmaan Janhangeer
pythonmembers.club  | github

Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 3rd party mail package

2019-12-13 Thread Brian Oney via Python-list
How about a 1st party package in the stdlib?
>From the hip: Take an example or two from the 'python 2 or 3 standard library 
>by example' book by a guy named Doug.

Hth (really) 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 3rd party mail package

2019-12-13 Thread Abdur-Rahmaan Janhangeer
Wonder where the 2nd party went to. Thanks will look into it!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 3rd party mail package

2019-12-13 Thread Abdur-Rahmaan Janhangeer
That's my starting point au fait. Was asking for a 3rd party package. gmail
from pypi has a nice api 👌 (filter etc seem awesome). Wondered if
something similar exists.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-13 Thread boB Stepp
On Wed, Dec 11, 2019 at 6:51 PM Greg Ewing  wrote:
>
> On 12/12/19 3:50 am, R.Wieser wrote:
> > I was rather clear about what my used version of Python was-and-is.  I have
> > no idea why that was ignored. :-(
>
> I think we've all been talking at cross purposes for a while.
>
> What I meant to say originally was that you were relying on
> an implementation detail of current CPython, and your code
> might not work on other versions of Python. I was just
> trying to make you aware of something that is considered
> "best practice" when writing Python code, for your own
> future reference.

A question:  As these behaviors being discussed are CPython
implementation details, is it true that CPython itself is free to
implement these behaviors differently in its own future versions?



-- 
boB
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3 - How do I import a class from another file

2019-12-13 Thread Greg Ewing

On 14/12/19 5:13 pm, boB Stepp wrote:

is it true that CPython itself is free to
implement these behaviors differently in its own future versions?


Yes. That's why it's not a good idea to rely on them even
if you only intend to run your code on CPython.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list