user1911678
user1911678

Reputation: 11

python HTTP multipart header value changed after using urllib.request.Request()

When sending HTTP POST, the header "connection" value which was set to keep-alive, becomes 'close' in the outgoing packet.

Here is the header I'm using:

multipart_header = {        
                        'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Firefox/17.0',
                        'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                        'accept-language':'en-US,en;q=0.5',
                        'accept-encoding':'gzip, deflate',
                        'connection':'keep-alive',

                        'content-type':'multipart/form-data; boundary='+boundary,
                        'content-length':''
        }


## command to send the header: 
urllib.request.Request('http://localhost/api/image/upload', data=byte_data, headers=multipart_header)

When i capture the POST packet I can see that the connection field becomes "closed" instead of the expected "keep-alive". What is going on here?

Upvotes: 0

Views: 390

Answers (2)

rkday
rkday

Reputation: 1146

http://docs.python.org/dev/library/urllib.request.html says:

urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests.

Presumably this makes sense - I assume urllib.request doesn't have any capabilities to actually store the TCP socket for a real keepalive connection, so you can't override this header.

https://github.com/psf/requests seems to support it, though I haven't used this.

Upvotes: 1

jfs
jfs

Reputation: 414475

urllib doesn't support persistent connections. If you already have headers and data to send then you could use http.client to reuse http connection:

from http.client import HTTPConnection

conn = HTTPConnection('localhost', strict=True)
conn.request('POST', '/api/image/upload', byte_data, headers)
r = conn.getresponse()
r.read() # should read before sending the next request
# conn.request(...

See Persistence of urllib.request connections to a HTTP server.

Upvotes: 0

Related Questions