Skip to content

Commit 2e059f0

Browse files
authored
Add files via upload
1 parent 909e0ac commit 2e059f0

8 files changed

Lines changed: 736 additions & 352 deletions

PyWWW/pywwwget_chatgpt.py

Lines changed: 92 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,9 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
18651865
p = urlparse(url)
18661866
username = unquote(p.username) if p.username else None
18671867
password = unquote(p.password) if p.password else None
1868-
1868+
if(httpmethod is None):
1869+
httpmethod = "GET"
1870+
httpmethod = httpmethod.upper()
18691871
# Strip auth from URL
18701872
netloc = p.hostname or ""
18711873
if p.port:
@@ -1916,13 +1918,13 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
19161918
# Requests
19171919
if usehttp == "requests" and haverequests:
19181920
auth = (username, password) if (username and password) else None
1919-
extendargs.update({'url': rebuilt_url, 'headers': headers, 'auth': auth, 'cookies': httpcookie, 'stream': True, 'timeout': (float(timeout), float(timeout))})
1921+
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'headers': headers, 'auth': auth, 'cookies': httpcookie, 'stream': True, 'allow_redirects': True, 'timeout': (float(timeout), float(timeout))})
19201922
try:
1921-
if(httpmethod == "GET"):
1922-
extendargs.update({'method': "GET"})
1923-
elif(httpmethod == "POST"):
1924-
extendargs.update({'method': "POST"})
1925-
if(sendfiles is not None):
1923+
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)):
19261928
jsonpost = False
19271929
sendfiles = to_requests_files(sendfiles)
19281930
if(sendfiles is not None):
@@ -1934,8 +1936,22 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
19341936
extendargs.update({'json': postdata})
19351937
elif(not jsonpost and postdata is not None):
19361938
extendargs.update({'data': postdata})
1937-
else:
1938-
extendargs.update({'method': "GET"})
1939+
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)):
1944+
jsonpost = False
1945+
sendfiles = to_requests_files(sendfiles)
1946+
if(sendfiles is not None):
1947+
for _, (_, fobj, *_) in sendfiles:
1948+
if hasattr(fobj, "seek"):
1949+
fobj.seek(0)
1950+
extendargs.update({'files': sendfiles})
1951+
if(jsonpost and postdata is not None):
1952+
extendargs.update({'json': postdata})
1953+
elif(not jsonpost and postdata is not None and (isinstance(sendfiles, dict) or sendfiles is None)):
1954+
extendargs.update({'data': postdata})
19391955
r = requests.request(**extendargs)
19401956
r.raise_for_status()
19411957
except requests.exceptions.HTTPError as e:
@@ -1972,12 +1988,28 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
19721988
try:
19731989
with httpx.Client(follow_redirects=True, http1=True, http2=usehttp2, trust_env=True, timeout=float(timeout)) as client:
19741990
auth = (username, password) if (username and password) else None
1975-
extendargs.update({'url': rebuilt_url, 'headers': headers, 'auth': auth, 'cookies': httpcookie})
1976-
if(httpmethod == "GET"):
1977-
extendargs.update({'method': "GET"})
1978-
elif(httpmethod == "POST"):
1979-
extendargs.update({'method': "POST"})
1980-
if(sendfiles is not None):
1991+
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'headers': headers, 'auth': auth, 'cookies': httpcookie})
1992+
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+
jsonpost = False
1998+
sendfiles = to_requests_files(sendfiles)
1999+
if(sendfiles is not None):
2000+
for _, (_, fobj, *_) in sendfiles:
2001+
if hasattr(fobj, "seek"):
2002+
fobj.seek(0)
2003+
extendargs.update({'files': sendfiles})
2004+
if(jsonpost and postdata is not None):
2005+
extendargs.update({'json': postdata})
2006+
elif(not jsonpost and postdata is not None):
2007+
extendargs.update({'data': postdata})
2008+
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)):
19812013
jsonpost = False
19822014
sendfiles = to_requests_files(sendfiles)
19832015
if(sendfiles is not None):
@@ -1989,8 +2021,6 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
19892021
extendargs.update({'json': postdata})
19902022
elif(not jsonpost and postdata is not None):
19912023
extendargs.update({'data': postdata})
1992-
else:
1993-
extendargs.update({'method': "GET"})
19942024
r = client.request(**extendargs)
19952025
r.raise_for_status()
19962026
except httpx.HTTPStatusError as e:
@@ -2026,31 +2056,24 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
20262056
except ImportError:
20272057
usehttp2 = False
20282058
with httpcore.ConnectionPool(http1=True, http2=usehttp2) as client:
2029-
if(httpmethod == "GET"):
2030-
httpcorem = "GET"
2031-
content = None
2032-
if(httpmethod == "POST"):
2033-
httpcorem = "POST"
2034-
content = postdata
2035-
else:
2036-
httpcorem = "GET"
2037-
content = None
20382059
timeoutdict = {"connect": float(timeout), "read": float(timeout), "write": float(timeout), "pool": float(timeout)}
2039-
extendargs.update({'url': rebuilt_url, 'extensions': {"timeout": timeoutdict}})
2040-
if(httpmethod == "GET"):
2041-
extendargs.update({'method': "GET"})
2042-
elif(httpmethod == "POST"):
2043-
extendargs.update({'method': "POST"})
2044-
if(jsonpost and postdata is not None):
2060+
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'extensions': {"timeout": timeoutdict}})
2061+
if(httpmethod == "POST" or httpmethod == "PUT" or httpmethod == "PATCH" or httpmethod == "DELETE"):
2062+
if(jsonpost and postdata is not None and sendfiles is None):
20452063
if('Content-Type' in headers):
20462064
headers['Content-Type'] = "application/json"
20472065
else:
20482066
headers.update({'Content-Type': "application/json"})
20492067
extendargs.update({'content': json.dumps(postdata).encode('UTF-8')})
2050-
elif(not jsonpost and postdata is not None):
2068+
elif(not jsonpost and postdata is not None and sendfiles is None):
2069+
if('Content-Type' in headers):
2070+
headers['Content-Type'] = "application/x-www-form-urlencoded"
2071+
else:
2072+
headers.update({'Content-Type': "application/x-www-form-urlencoded"})
20512073
extendargs.update({'content': urlencode(postdata).encode('UTF-8')})
2052-
else:
2053-
extendargs.update({'method': "GET"})
2074+
elif(sendfiles is not None):
2075+
sendfiles.seek(0, 0)
2076+
extendargs.update({'content': sendfiles.read()})
20542077
extendargs.update({'headers': headers})
20552078
try:
20562079
with client.stream(**extendargs, ) as r:
@@ -2127,12 +2150,31 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
21272150
headers.update(auth_headers)
21282151
# Request with preload_content=False to get a file-like object
21292152
try:
2130-
extendargs.update({'url': rebuilt_url, 'headers': headers, 'preload_content': False, 'decode_content': True})
2131-
if(httpmethod == "GET"):
2132-
extendargs.update({'method': "GET"})
2133-
elif(httpmethod == "POST"):
2134-
extendargs.update({'method': "POST"})
2135-
if(sendfiles is not None):
2153+
extendargs.update({'url': rebuilt_url, 'method': httpmethod, 'headers': headers, 'preload_content': False, 'decode_content': True})
2154+
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)):
2159+
jsonpost = False
2160+
sendfiles = to_requests_files(sendfiles)
2161+
if(sendfiles is not None):
2162+
for _, (_, fobj, *_) in sendfiles:
2163+
if hasattr(fobj, "seek"):
2164+
fobj.seek(0)
2165+
extendargs.update({'fields': sendfiles})
2166+
if(jsonpost and postdata is not None):
2167+
extendargs.update({'json': postdata})
2168+
elif(not jsonpost and postdata is not None):
2169+
if('fields' in headers):
2170+
extendargs['fields'].update({postdata})
2171+
else:
2172+
extendargs.update({'fields': postdata})
2173+
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)):
21362178
jsonpost = False
21372179
sendfiles = to_requests_files(sendfiles)
21382180
if(sendfiles is not None):
@@ -2147,8 +2189,6 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
21472189
extendargs['fields'].update({postdata})
21482190
else:
21492191
extendargs.update({'fields': postdata})
2150-
else:
2151-
extendargs.update({'method': "GET"})
21522192
resp = http.request(**extendargs)
21532193
except (socket.timeout, socket.gaierror, urllib3.exceptions.MaxRetryError):
21542194
return False
@@ -2204,6 +2244,11 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
22042244
curlreq.setopt(pycurl.POSTFIELDS, json.dumps(postdata).encode('UTF-8'))
22052245
elif(not jsonpost and postdata is not None):
22062246
curlreq.setopt(pycurl.POSTFIELDS, urlencode(postdata).encode('UTF-8'))
2247+
elif(httpmethod == "PUT" or httpmethod == "PATCH"):
2248+
curlreq.setopt(pycurl.CUSTOMREQUEST, httpmethod)
2249+
curlreq.setopt(pycurl.UPLOAD, True)
2250+
sendfiles.seek(0, 0)
2251+
curlreq.setopt(pycurl.READDATA, sendfiles)
22072252
else:
22082253
curlreq.setopt(pycurl.HTTPGET, True)
22092254
headers = make_http_headers_from_dict_to_pycurl(headers)
@@ -2319,7 +2364,10 @@ def download_file_from_http_file(url, headers=None, usehttp=__use_http_lib__, ht
23192364
fulldatasize, 2, "IEC"), 'SI': get_readable_size(fulldatasize, 2, "SI")}, 'Headers': httpheaderout, 'Version': httpversionout, 'Method': httpmethodout, 'HeadersSent': httpheadersentout, 'URL': httpurlout, 'Code': httpcodeout, 'Reason': httpcodereason, 'HTTPLib': usehttp, 'RequestTime': {'StartTime': start_time, 'EndTime': end_time, 'TotalTime': total_time}}
23202365
return returnval
23212366
else:
2322-
return httpfile
2367+
if(httpmethod == "HEAD"):
2368+
return httpheadersentout
2369+
else:
2370+
return httpfile
23232371

23242372
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):
23252373
fp = download_file_from_http_file(url, headers, usehttp, httpuseragent, httpreferer, httpcookie, httpmethod, postdata, jsonpost, sendfiles, timeout, returnstats)

0 commit comments

Comments
 (0)