These are chat archives for cherrypy/cherrypy

3rd
Jan 2019
杨世昭
@maroon13
Jan 03 03:12
Hi everyone
I want to use cherrypy to receive an image file But I don't know how can I do
Sviatoslav Sydorenko
@webknjaz
Jan 03 09:20
@jaraco we need to start updating links to projects using CherryPy in the wild: https://www.spiderfoot.net/documentation/#reqs
@maroon13 you can access incoming stream via file descriptor at cherrypy.request.body
杨世昭
@maroon13
Jan 03 10:25
image.png
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:26
please use actual text instead of pictures
杨世昭
@maroon13
Jan 03 10:27
@cherrypy.config(**{'response.timeout': 3600}) # default is 300s
@cherrypy.expose()
def uploadImageData(self, files):
'''Handle non-multipart upload'''
destination = os.path.join('/home/shcmq/sewage/pc/static/images')
with open(destination, 'wb') as f:
  shutil.copyfileobj(cherrypy.request.body, f)

return 'Okay'
return HTTPError: (404, 'Missing parameters: files')
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:27
you can edit it to use triple backticks at the beginning and the end of the snippet ```
it'd be better readable this way
remove files arg if you don't pass it in HTTP request
杨世昭
@maroon13
Jan 03 10:31
At start I want to use files['typeName'] to choose but I can't get the file stream
I only get a file name
so I use cherrypy.request.body
@cherrypy.expose
def uploadImageData(self, **args):
fileType = args['imageFile'].split('.')[1]
now = datetime.datetime.now()
nowDate = now.strftime('%Y-%m-%d %H:%M:%S')
destination = os.path.join('/home/shcmq/sewage/pc/static/images/' + nowDate + '.' + fileType)
with open(destination, 'wb') as f:
shutil.copyfileobj(cherrypy.request.body, f)
return 'Okay'
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:34
please use tripple backticks so that it'd be readable
like this
def hello():
    return 'world'
and I don't really see what's your problem here
杨世昭
@maroon13
Jan 03 10:35
OK
@cherrypy.expose
def uploadImageData(self, **args):
fileType = args['imageFile'].split('.')[1]
now = datetime.datetime.now()
nowDate = now.strftime('%Y-%m-%d %H:%M:%S')
destination = os.path.join('/home/shcmq/sewage/pc/static/images/' + nowDate + '.' + fileType)
with open(destination, 'wb') as f:
print cherrypy.request.body, 'ffffffffffffffff'
shutil.copyfileobj(cherrypy.request.body, f)
return 'Okay'
four backs - -
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:37
``` <- use these
杨世昭
@maroon13
Jan 03 10:37
sorry for that
I will try
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:37
three backticks at the beginning and at the end
you can try copy-pasting -> ```
杨世昭
@maroon13
Jan 03 10:37
  def uploadImageData(self, **args):
    fileType = args['imageFile'].split('.')[1]
    now = datetime.datetime.now()
    nowDate = now.strftime('%Y-%m-%d %H:%M:%S')
    destination = os.path.join('/home/shcmq/sewage/pc/static/images/' + nowDate + '.' + fileType)
    with open(destination, 'wb') as f:
      print cherrypy.request.body, 'ffffffffffffffff'
      shutil.copyfileobj(cherrypy.request.body, f)

    return 'Okay'
thank you
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:37
great thanks
so what's the problem?
杨世昭
@maroon13
Jan 03 10:38
I upload an image file and save it to my path
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:38
so?
1 image?
杨世昭
@maroon13
Jan 03 10:38
but I got a 0Kb file
1 image
single file upload
-rw-rw-r-- 1 ysz ysz 0 Jan 3 18:42 2019-01-03 18:42:03.png
this is the file infomation
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:46
do what does it print to the terminal?
ah, okay, you should be able to access file descriptor from the handler arg as well. but use the file filed correctly
how do you send the HTTP query?
which client?
I've just tested a small example and it works
杨世昭
@maroon13
Jan 03 10:50
<form action="/otherApi/uploadImageData" method="post"> <input type="file"> <input type="submit"> </form>
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:50
you should give it a name
and you probably need a multipart form
杨世昭
@maroon13
Jan 03 10:51
at first I use this<input type="file" name="imageFile">
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:51
okay, so your handler should use imageFile as an argument
杨世昭
@maroon13
Jan 03 10:53
def uploadImageData(self, imageFile): like this?
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:53
yes
here's my example (warning: uses some of python 3.6+ syntax):
#! /usr/bin/env python3

__metaclass__ = type

import cherrypy


config = {
    'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 9090,
        'server.thread_pool' : 8,
        'server.max_request_body_size' : 0,
        'server.socket_timeout' : 60
    }
}


class App:

    @cherrypy.expose
    def upload(self, ufile):
        size = 0
        while True:
            data = ufile.file.read(8192)
            if not data:
                break
            size += len(data)
        out = f"""
length: {size}
filename: {ufile.filename}
mime-type: {data}

incoming headers: {cherrypy.request.headers}
"""
        return out


if __name__ == '__main__':
    cherrypy.quickstart(App(), '/', config)
$ curl -v -F ufile=@native_cp.py http://localhost:9090/upload
*   Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 9090 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9090 (#0)
> POST /upload HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.62.0
> Accept: */*
> Content-Length: 597
> Content-Type: multipart/form-data; boundary=------------------------7aa5464f0c6d9d6a
> 
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Server: CherryPy/18.1.0
< Date: Thu, 03 Jan 2019 10:55:11 GMT
< Content-Length: 286
< 

length: 392
filename: native_cp.py
mime-type: b''

incoming headers: {'Remote-Addr': '127.0.0.1', 'Host': 'localhost:9090', 'User-Agent': 'curl/7.62.0', 'Accept': '*/*', 'Content-Type': 'multipart/form-data; boundary=------------------------7aa5464f0c6d9d6a', 'Content-Length': '597'}
* Connection #0 to host localhost left intact
杨世昭
@maroon13
Jan 03 10:57
@webknjaz thank you now I will get a try
Sviatoslav Sydorenko
@webknjaz
Jan 03 10:59
So you'll probably need smth like <form action="/uploadImageData" method="post" enctype="multipart/form-data"> in your app, because enctype's default value is application/x-www-form-urlencoded which is probably not what you want
杨世昭
@maroon13
Jan 03 11:00
yeah I will add it
Sviatoslav Sydorenko
@webknjaz
Jan 03 11:00
:+1:
杨世昭
@maroon13
Jan 03 11:00
thanks a lot
Sviatoslav Sydorenko
@webknjaz
Jan 03 11:00
you are welcome :)
Jason R. Coombs
@jaraco
Jan 03 13:36
@webknjaz Personally, I like color in the logo… and I prefer two cherries to just one. But I don’t feel strongly about it. I’d be happy with any selection.
Sviatoslav Sydorenko
@webknjaz
Jan 03 14:42
I feel like that monochrome one is more stylish tho
Should we ask extra people about their opinions? Previous maintainers maybe?
Maybe it's not the case for current state of CherryPy but when considering a logo it's usually important that it could be put on different types of media (web vs offline use), easily inverted (to be put on different background), easy to draw by hand and have recognizable monochromatic version (print primarily). That's the difference of a logo from just a picture.
Jason R. Coombs
@jaraco
Jan 03 15:59
The monochrome one is fine. Let’s go with that.