httplib

HTTPS POST request Python, returning .csv

风格不统一 提交于 2019-12-06 07:57:08
I want to make a post request to a HTTPS-site that should respond with a .csv file. I have this Python code: try: #conn = httplib.HTTPSConnection(host="www.site.com", port=443) => Gives an BadStatusLine: ' ' error conn = httplib.HTTPConnection("www.site.com"); params = urllib.urlencode({'val1':'123','val2':'abc','val3':'1b3'}) conn.request("POST", "/nps/servlet/exportdatadownload", params) content = conn.getresponse() print content.reason, content.status print content.read() conn.close() except: import sys print sys.exc_info()[:2] Output: Found 302 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0/

multiple requests in a single connection?

假装没事ソ 提交于 2019-12-05 23:01:10
Is it possible to put multiple requests without breaking the connection using python httplib?. Like, can I upload a big file to the server in parts but in a single socket connection. I looked for answers. But nothing seemed so clear and definite. Any examples/related links will be helpfull. Thanks. Yes, the connection stays open until you close it using the close() method. The following example, taken from the httplib documentation , shows how to perform multiple requests using a single connection: >>> import httplib >>> conn = httplib.HTTPConnection("www.python.org") >>> conn.request("GET", "

does httplib reuse TCP connections? [duplicate]

泪湿孤枕 提交于 2019-12-05 17:51:55
This question already has an answer here: How to Speed Up Python's urllib2 when doing multiple requests 3 answers I'm using httplib to grab bunch of resources from a website and i want it at minimum cost, so i set 'Connection: keep-alive' HTTP header on my requests but i'm not sure it actually uses the same TCP connection for as many requests as the webserver allows. i = 0 while 1: i += 1 print i con = httplib.HTTPConnection("myweb.com") con.request("GET", "/x.css", headers={"Connection":" keep-alive"}) result = con.getresponse() print result.reason, result.getheaders() Is my implementation

How do I use Python's httplib to send a POST to a URL, with a dictionary of parameters?

旧城冷巷雨未停 提交于 2019-12-05 09:57:38
问题 I just want a function that can take 2 parameters: the URL to POST to a dictionary of parameters How can this be done with httplib? thanks. 回答1: From the Python documentation: >>> import httplib, urllib >>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> headers = {"Content-type": "application/x-www-form-urlencoded", ... "Accept": "text/plain"} >>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80") >>> conn.request("POST", "/cgi-bin/query", params, headers) >>> response

Upload a file with python using httplib

对着背影说爱祢 提交于 2019-12-04 19:36:20
conn = httplib.HTTPConnection("www.encodable.com/uploaddemo/") conn.request("POST", path, chunk, headers) Above is the site "www.encodable.com/uploaddemo/" where I want to upload an image. I am better versed in php so I am unable to understand the meaning of path and headers here. In the code above, chunk is an object consisting of my image file. The following code produces an error as I was trying to implement without any knowledge of headers and path. import httplib def upload_image_to_url(): filename = '//home//harshit//Desktop//h1.jpg' f = open(filename, "rb") chunk = f.read() f.close()

httplib is not getting all the redirect codes

杀马特。学长 韩版系。学妹 提交于 2019-12-04 16:58:38
I am trying to get the final url of a page that seems to redirect more than once. Try this sample URL in your browser and compare it to the final URL at the bottom of my code snippet: Link that redirects more than once And here is the test code I was running, notice the final URL that gets a code of 200 isn't the same as the one in your browser. What are my options? Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import httplib >>> from urlparse import urlparse >>> url = 'http://www.usmc.mil

How to send Multipart/related requests in Python to SOAP server?

风流意气都作罢 提交于 2019-12-04 16:02:39
I have to send a file to a SOAP server via a multipart/related HTTP POST. I have built the message from scratch like this: from email.mime.application import MIMEApplication from email.encoders import encode_7or8bit from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase envelope = """<?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xop="http

How to handle multiple Set-Cookie header in HTTP response

£可爱£侵袭症+ 提交于 2019-12-04 12:50:18
I'm trying to write simple proxy server for some purpose. In it I use httplib to access remote web-server. But there's one problem: web server returns TWO Set-Cookie headers in one response, and httplib mangles them together in httplib.HTTPResponse.getheaders(), effectively joining cookies with comma [which is strange, because getheaders returns a LIST, not DICT, so I thought they wrote it with multiple headers of the same name). So, when I send this joined header back to client, it confuses client. How can I obtain full list of headers in httplib (without just splitting Set-Cookie header on

Python httplib and POST

元气小坏坏 提交于 2019-12-04 11:44:38
问题 I am currently working with a piece of code that has been written by somebody else. It uses httplib to make requests to server. It has all the data supplied in a correct format - for example message body, header values, etc. The problem is that each time it attempts to send a POST requests, the data is there - I can see it on the client side, however nothing arrives to the server. I've read through the library specification and the usage seems to be correct. The extracted library calls go as

How do I make a PATCH request in Python?

和自甴很熟 提交于 2019-12-04 02:46:56
问题 Is there a way to make a request using the PATCH HTTP method in Python? I tried using httplib, but it doesn't accept PATCH as method param. 回答1: With Requests, making PATCH requests is very simple: import requests r = requests.patch('http://httpbin.org/patch') 回答2: Seems to work in 2.7.1 as well. >>> import urllib2 >>> request = urllib2.Request('http://google.com') >>> request.get_method = lambda: 'PATCH' >>> resp = urllib2.urlopen(request) Traceback (most recent call last): ... urllib2