Skip to content

Commit f329be5

Browse files
authored
Add files via upload
1 parent 2e059f0 commit f329be5

8 files changed

Lines changed: 568 additions & 320 deletions

PyWWW/pywwwget_chatgpt.py

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,7 +1856,7 @@ def to_pycurl_httpost(payload, default_ext=".txt"):
18561856

18571857
return http_post
18581858

1859-
def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, timeout=60, returnstats=False):
1859+
def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, putfile=None, timeout=60, returnstats=False):
18601860
if headers is None:
18611861
headers = {}
18621862
else:
@@ -1921,10 +1921,12 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
19211921
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'headers': headers, 'auth': auth, 'cookies': httpcookie, 'stream': True, 'allow_redirects': True, 'timeout': (float(timeout), float(timeout))})
19221922
try:
19231923
if(httpmethod == "POST"):
1924-
if(sendfiles is not None and not isinstance(sendfiles, dict)):
1925-
sendfiles.seek(0, 0)
1926-
extendargs.update({'data': sendfiles})
1927-
elif(sendfiles is not None and isinstance(sendfiles, dict)):
1924+
if(putfile is not None and sendfiles is not None):
1925+
putfile = None
1926+
if(putfile is not None):
1927+
putfile.seek(0, 0)
1928+
extendargs.update({'data': putfile})
1929+
if(sendfiles is not None and isinstance(sendfiles, dict)):
19281930
jsonpost = False
19291931
sendfiles = to_requests_files(sendfiles)
19301932
if(sendfiles is not None):
@@ -1937,10 +1939,12 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
19371939
elif(not jsonpost and postdata is not None):
19381940
extendargs.update({'data': postdata})
19391941
elif(httpmethod == "PUT" or httpmethod == "PATCH" or httpmethod == "DELETE"):
1940-
if(sendfiles is not None and not isinstance(sendfiles, dict)):
1941-
sendfiles.seek(0, 0)
1942-
extendargs.update({'data': sendfiles})
1943-
elif(sendfiles is not None and isinstance(sendfiles, dict)):
1942+
if(putfile is not None and sendfiles is not None):
1943+
sendfiles = None
1944+
if(putfile is not None):
1945+
putfile.seek(0, 0)
1946+
extendargs.update({'data': putfile})
1947+
if(sendfiles is not None and isinstance(sendfiles, dict)):
19441948
jsonpost = False
19451949
sendfiles = to_requests_files(sendfiles)
19461950
if(sendfiles is not None):
@@ -1990,10 +1994,12 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
19901994
auth = (username, password) if (username and password) else None
19911995
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'headers': headers, 'auth': auth, 'cookies': httpcookie})
19921996
if(httpmethod == "POST"):
1993-
if(sendfiles is not None and not isinstance(sendfiles, dict)):
1994-
sendfiles.seek(0, 0)
1995-
extendargs.update({'content': sendfiles})
1996-
elif(sendfiles is not None and isinstance(sendfiles, dict)):
1997+
if(putfile is not None and sendfiles is not None):
1998+
putfile = None
1999+
if(putfile is not None):
2000+
putfile.seek(0, 0)
2001+
extendargs.update({'content': putfile})
2002+
if(sendfiles is not None and isinstance(sendfiles, dict)):
19972003
jsonpost = False
19982004
sendfiles = to_requests_files(sendfiles)
19992005
if(sendfiles is not None):
@@ -2006,10 +2012,12 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
20062012
elif(not jsonpost and postdata is not None):
20072013
extendargs.update({'data': postdata})
20082014
elif(httpmethod == "PUT" or httpmethod == "PATCH" or httpmethod == "DELETE"):
2009-
if(sendfiles is not None and not isinstance(sendfiles, dict)):
2010-
sendfiles.seek(0, 0)
2011-
extendargs.update({'content': sendfiles})
2012-
elif(sendfiles is not None and isinstance(sendfiles, dict)):
2015+
if(putfile is not None and sendfiles is not None):
2016+
sendfiles = None
2017+
if(putfile is not None):
2018+
putfile.seek(0, 0)
2019+
extendargs.update({'content': putfile})
2020+
if(sendfiles is not None and isinstance(sendfiles, dict)):
20132021
jsonpost = False
20142022
sendfiles = to_requests_files(sendfiles)
20152023
if(sendfiles is not None):
@@ -2059,21 +2067,21 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
20592067
timeoutdict = {"connect": float(timeout), "read": float(timeout), "write": float(timeout), "pool": float(timeout)}
20602068
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'extensions': {"timeout": timeoutdict}})
20612069
if(httpmethod == "POST" or httpmethod == "PUT" or httpmethod == "PATCH" or httpmethod == "DELETE"):
2062-
if(jsonpost and postdata is not None and sendfiles is None):
2070+
if(jsonpost and postdata is not None and putfile is None):
20632071
if('Content-Type' in headers):
20642072
headers['Content-Type'] = "application/json"
20652073
else:
20662074
headers.update({'Content-Type': "application/json"})
20672075
extendargs.update({'content': json.dumps(postdata).encode('UTF-8')})
2068-
elif(not jsonpost and postdata is not None and sendfiles is None):
2076+
elif(not jsonpost and postdata is not None and putfile is None):
20692077
if('Content-Type' in headers):
20702078
headers['Content-Type'] = "application/x-www-form-urlencoded"
20712079
else:
20722080
headers.update({'Content-Type': "application/x-www-form-urlencoded"})
20732081
extendargs.update({'content': urlencode(postdata).encode('UTF-8')})
2074-
elif(sendfiles is not None):
2075-
sendfiles.seek(0, 0)
2076-
extendargs.update({'content': sendfiles.read()})
2082+
elif(putfile is not None):
2083+
putfile.seek(0, 0)
2084+
extendargs.update({'content': putfile})
20772085
extendargs.update({'headers': headers})
20782086
try:
20792087
with client.stream(**extendargs, ) as r:
@@ -2151,11 +2159,15 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
21512159
# Request with preload_content=False to get a file-like object
21522160
try:
21532161
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'headers': headers, 'preload_content': False, 'decode_content': True})
2162+
if(putfile is not None and sendfiles is not None):
2163+
sendfiles = None
21542164
if(httpmethod == "POST"):
2155-
if(sendfiles is not None and not isinstance(sendfiles, dict)):
2156-
sendfiles.seek(0, 0)
2157-
extendargs.update({'body': sendfiles})
2158-
elif(sendfiles is not None and isinstance(sendfiles, dict)):
2165+
if(putfile is not None and sendfiles is not None):
2166+
putfile = None
2167+
if(putfile is not None and not isinstance(putfile, dict)):
2168+
putfile.seek(0, 0)
2169+
extendargs.update({'body': putfile})
2170+
if(sendfiles is not None and isinstance(sendfiles, dict)):
21592171
jsonpost = False
21602172
sendfiles = to_requests_files(sendfiles)
21612173
if(sendfiles is not None):
@@ -2171,10 +2183,12 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
21712183
else:
21722184
extendargs.update({'fields': postdata})
21732185
elif(httpmethod == "PUT" or httpmethod == "PATCH" or httpmethod == "DELETE"):
2174-
if(sendfiles is not None and not isinstance(sendfiles, dict)):
2175-
sendfiles.seek(0, 0)
2176-
extendargs.update({'body': sendfiles})
2177-
elif(sendfiles is not None and isinstance(sendfiles, dict)):
2186+
if(putfile is not None and sendfiles is not None):
2187+
sendfiles = None
2188+
if(putfile is not None and not isinstance(putfile, dict)):
2189+
putfile.seek(0, 0)
2190+
extendargs.update({'body': putfile})
2191+
if(sendfiles is not None and isinstance(sendfiles, dict)):
21782192
jsonpost = False
21792193
sendfiles = to_requests_files(sendfiles)
21802194
if(sendfiles is not None):
@@ -2231,6 +2245,8 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
22312245
if(httpmethod == "GET"):
22322246
curlreq.setopt(pycurl.HTTPGET, True)
22332247
elif(httpmethod == "POST"):
2248+
if(putfile is not None and sendfiles is not None):
2249+
putfile = None
22342250
curlreq.setopt(pycurl.POST, True)
22352251
if(sendfiles is not None):
22362252
jsonpost = False
@@ -2244,11 +2260,26 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
22442260
curlreq.setopt(pycurl.POSTFIELDS, json.dumps(postdata).encode('UTF-8'))
22452261
elif(not jsonpost and postdata is not None):
22462262
curlreq.setopt(pycurl.POSTFIELDS, urlencode(postdata).encode('UTF-8'))
2247-
elif(httpmethod == "PUT" or httpmethod == "PATCH"):
2263+
elif(httpmethod == "PUT" or httpmethod == "PATCH" or httpmethod == "DELETE"):
2264+
if(putfile is not None and sendfiles is not None):
2265+
sendfiles = None
22482266
curlreq.setopt(pycurl.CUSTOMREQUEST, httpmethod)
2249-
curlreq.setopt(pycurl.UPLOAD, True)
2250-
sendfiles.seek(0, 0)
2251-
curlreq.setopt(pycurl.READDATA, sendfiles)
2267+
if(putfile is not None):
2268+
curlreq.setopt(pycurl.UPLOAD, True)
2269+
putfile.seek(0, 0)
2270+
curlreq.setopt(pycurl.READDATA, putfile)
2271+
if(sendfiles is not None):
2272+
jsonpost = False
2273+
sendfiles = to_pycurl_httpost(sendfiles)
2274+
curlreq.setopt(pycurl.HTTPPOST, sendfiles)
2275+
if(jsonpost and postdata is not None):
2276+
if('Content-Type' in headers):
2277+
headers['Content-Type'] = "application/json"
2278+
else:
2279+
headers.update({'Content-Type': "application/json"})
2280+
curlreq.setopt(pycurl.POSTFIELDS, json.dumps(postdata).encode('UTF-8'))
2281+
elif(not jsonpost and postdata is not None):
2282+
curlreq.setopt(pycurl.POSTFIELDS, urlencode(postdata).encode('UTF-8'))
22522283
else:
22532284
curlreq.setopt(pycurl.HTTPGET, True)
22542285
headers = make_http_headers_from_dict_to_pycurl(headers)
@@ -2369,15 +2400,15 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
23692400
else:
23702401
return httpfile
23712402

2372-
def download_file_from_http_string(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, timeout=60, returnstats=False):
2373-
fp = download_file_from_http_file(url, headers, usehttp, httpuseragent, httpreferer, httpcookie, httpmethod, postdata, jsonpost, sendfiles, timeout, returnstats)
2403+
def download_file_from_http_string(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, putfile=None, timeout=60, returnstats=False):
2404+
fp = download_file_from_http_file(url, headers, usehttp, httpuseragent, httpreferer, httpcookie, httpmethod, postdata, jsonpost, sendfiles, putfile, timeout, returnstats)
23742405
return fp.read() if fp else False
23752406

2376-
def download_file_from_https_string(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, timeout=60, returnstats=False):
2377-
return download_file_from_http_file(url, headers, usehttp, httpuseragent, httpreferer, httpcookie, httpmethod, postdata, jsonpost, sendfiles, timeout, returnstats)
2407+
def download_file_from_https_string(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, putfile=None, timeout=60, returnstats=False):
2408+
return download_file_from_http_file(url, headers, usehttp, httpuseragent, httpreferer, httpcookie, httpmethod, postdata, jsonpost, sendfiles, putfile, timeout, returnstats)
23782409

2379-
def download_file_from_https_string(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, timeout=60, returnstats=False):
2380-
return download_file_from_http_string(url, headers, usehttp, httpuseragent, httpreferer, httpcookie, httpmethod, postdata, jsonpost, sendfiles, timeout, returnstats)
2410+
def download_file_from_https_string(url, headers=None, usehttp=__use_http_lib__, httpuseragent=None, httpreferer=None, httpcookie=geturls_cj, httpmethod="GET", postdata=None, jsonpost=False, sendfiles=None, putfile=None, timeout=60, returnstats=False):
2411+
return download_file_from_http_string(url, headers, usehttp, httpuseragent, httpreferer, httpcookie, httpmethod, postdata, jsonpost, sendfiles, putfile, timeout, returnstats)
23812412

23822413
# --------------------------
23832414
# TCP/UDP transport (receiver + sender)

0 commit comments

Comments
 (0)