I am trying to read an excel file in the backened (flask) sent from the frontend (angularjs). However, I am getting the error
File "/var/task/pandas/util/_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "/var/task/pandas/io/excel/_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "/var/task/pandas/io/excel/_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "/var/task/pandas/io/excel/_openpyxl.py", line 480, in __init__
super().__init__(filepath_or_buffer)
File "/var/task/pandas/io/excel/_base.py", line 351, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "/var/task/pandas/io/excel/_openpyxl.py", line 491, in load_workbook
return load_workbook(
File "/mnt/accesspoint/openpyxl/reader/excel.py", line 315, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "/mnt/accesspoint/openpyxl/reader/excel.py", line 124, in __init__
self.archive = _validate_archive(fn)
File "/mnt/accesspoint/openpyxl/reader/excel.py", line 96, in _validate_archive
archive = ZipFile(filename, 'r')
File "/var/lang/lib/python3.8/zipfile.py", line 1269, in __init__
self._RealGetContents()
File "/var/lang/lib/python3.8/zipfile.py", line 1364, in _RealGetContents
raise BadZipFile("Bad magic number for central directory")
zipfile.BadZipFile: Bad magic number for central directory
What is the problem that's causing this?
Angularjs html
<nz-card nzTitle="Upload File" bordered={false} style="flex: 1 1 auto;">
<nz-upload
[nzCustomRequest]="handleUpload"
(nzChange)="handleChange($event)"
>
<p class="ant-upload-drag-icon"><i nz-icon nzType="inbox"></i></p>
<p class="ant-upload-text">
Click or drag CSV/Excel a file to this area to upload
</p>
</nz-upload>
</nz-card>
Angularjs component
handleUpload = (item: any) => {
const formData = new FormData();
formData.append(item.name, item.file);
const headers = new HttpHeaders();
headers.set('Content-Type', 'multipart/form-data');
var upload = this.httpClient.post(this.SERVER_URL, formData,{headers: headers}).subscribe(
(res) => {
console.log("success");
}
Flask python
def upload():
file = request.files['file']
print("request", file)
df = pd.read_excel(file, index_col=0, engine='openpyxl')
print(df)
Logs (before the error message)
request <FileStorage: 'sample_excel.xlsx' ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')>
I have also tried reading it using with
with open(file, 'rb') as f
But with that I get an error stating TypeError: expected str, bytes or os.PathLike object, not FileStorage
Any help would be greatly appreciated!!
Related
Here's my code:
import discord
from discord.ext import commands
from discord import app_commands
import tailer
import pyrcon
import geocoder
import time
Prefix = "-"
bot = commands.Bot(command_prefix=Prefix, case_insensitive = True, intents = discord.Intents.all())
#bot.event
async def on_ready():
print(bot.user)
await bot.change_presence(status=discord.Status.dnd)
for line in tailer.follow(open("/data/dm/main/connectlog.log")):
Name = line.split('%')[1]
IP = line.split('%')[3]
ip_address = geocoder.ip(IP)
country = ip_address.country
state = ip_address.state
city = ip_address.city
time.sleep(4)
if "\"connect" in line:
embed = discord.Embed(title = f"Player {Name} connected to SS-DM server", description=f"IP : {IP}\nCountry : {country}\nState/Province : {state}\n City : {city}", color=0x00FF00)
channel = bot.get_channel(ID)
await channel.send(embed = embed)
bot.run(TOKEN)
So when I run my above code it shows the following error:
File "/usr/local/lib/python3.10/dist-packages/discord/client.py", line 828, in run
asyncio.run(runner())
File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.10/asyncio/base_events.py", line 633, in run_until_complete
self.run_forever()
File "/usr/lib/python3.10/asyncio/base_events.py", line 600, in run_forever
self._run_once()
File "/usr/lib/python3.10/asyncio/base_events.py", line 1896, in _run_once
handle._run()
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/dist-packages/discord/client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "/data/dm/main/codwatcher1.py", line 65, in on_ready
for line in tailer.follow(open("/data/dm/main/connectlog.log")):
File "/usr/local/lib/python3.10/dist-packages/tailer/__init__.py", line 182, in follow
time.sleep(delay)
Basically I am trying to follow a file through tailer module of python and send it to discord. I tried everything but in vain, so I hope you guys can help me figure this out.
Its my gameserver log. To see who connected to my server..
I am using fsspec package to implement a function to read an https file.
_hostname = socket.gethostname()
proxy_auth = aiohttp.BasicAuth(_hostname, pwd)
of = fsspec.filesystem("https", client_kwargs={"trust_env":True, "auth":proxy_auth})
http_urls = ["https://stackoverflow.com/"]
print(of.cat(http_urls))
The above code is not fetching the content of the url. The stack trace for the above code is as follows:
File "C:\Anaconda3\envs\mvision\lib\site-packages\fsspec\asyn.py", line 91, in wrapper
return sync(self.loop, func, *args, **kwargs)
File "C:\Anaconda3\envs\mvision\lib\site-packages\fsspec\asyn.py", line 71, in sync
raise return_result
File "C:\Anaconda3\envs\mvision\lib\site-packages\fsspec\asyn.py", line 25, in _runner
result[0] = await coro
File "C:\Anaconda3\envs\mvision\lib\site-packages\fsspec\asyn.py", line 347, in _cat
raise ex
File "C:\Anaconda3\envs\mvision\lib\site-packages\fsspec\implementations\http.py", line 230, in _cat_file
async with session.get(url, **kw) as r:
File "C:\Anaconda3\envs\mvision\lib\site-packages\aiohttp\client.py", line 1117, in __aenter__
self._resp = await self._coro
File "C:\Anaconda3\envs\mvision\lib\site-packages\aiohttp\client.py", line 521, in _request
req, traces=traces, timeout=real_timeout
File "C:\Anaconda3\envs\mvision\lib\site-packages\aiohttp\connector.py", line 535, in connect
proto = await self._create_connection(req, traces, timeout)
File "C:\Anaconda3\envs\mvision\lib\site-packages\aiohttp\connector.py", line 892, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
File "C:\Anaconda3\envs\mvision\lib\site-packages\aiohttp\connector.py", line 1011, in _create_direct_connection
raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [getaddrinfo failed]
But if we wrap it with a cache, I am able to get the content.
_hostname = socket.gethostname()
proxy_auth = aiohttp.BasicAuth(_hostname, pwd)
of = fsspec.filesystem("simplecache", target_protocol="https", \
target_options={"client_kwargs":{"trust_env":True, "auth":proxy_auth}},
cache_storage="/tmp/files")
http_urls = ["https://stackoverflow.com/"]
print(of.cat(http_urls))
Why is the first code section not able to fetch the content? Am I doing anything wrong ?
So I asked this question and tried the ProcessPoolExecutor approach. I used the decorator suggested the following way:
Running Image Manipulation in run_in_executor. Adapting to multiprocessing
import asyncio
import functools
from concurrent import futures
from app.exceptions.errors import ManipulationError
_pool = futures.ProcessPoolExecutor()
def executor(function):
#functools.wraps(function)
def decorator(*args, **kwargs):
try:
partial = functools.partial(function, *args, **kwargs)
loop = asyncio.get_event_loop()
return loop.run_in_executor(_pool, partial)
except Exception as e:
raise ManipulationError(str(e))
return decorator
I then used it on a function like:
#executor
#pil
def blur(image):
frame = image.convert("RGBA")
return frame.filter(ImageFilter.BLUR)
Note the #pil is another decorator I made.
def pil(function):
#functools.wraps(function)
def wrapper(image, *args, **kwargs) -> BytesIO:
img = PILManip.pil_image(image)
if img.format == "GIF":
frames = []
for frame in ImageSequence.Iterator(img):
res_frame = function(frame, *args, **kwargs)
frames.append(res_frame)
return PILManip.pil_gif_save(frames), "gif"
elif img.format in ["PNG", "JPEG"]:
img = function(img, *args, **kwargs)
return PILManip.pil_image_save(img), "png"
else:
raise BadImage("Bad Format")
return wrapper
I called it in a FastApi route like so:
#router.get("/blur/", responses=normal_response)
async def blur_image(url: str):
byt = await Client.image_bytes(url)
img, image_format = await blur(byt)
return Response(img.read(), media_type=f"image/{image_format}")
I get some error about pickling.
500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 391, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/fastapi/applications.py", line 199, in __call__
await super().__call__(scope, receive, send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 25, in __call__
response = await self.dispatch_func(request, self.call_next)
File "/home/codespace/workspace/dagpi-image/app/middleware/timer.py", line 8, in add_process_time_header
response = await call_next(request)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 45, in call_next
task.result()
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 38, in coro
await self.app(scope, receive, send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 25, in __call__
response = await self.dispatch_func(request, self.call_next)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette_prometheus/middleware.py", line 56, in dispatch
raise e from None
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette_prometheus/middleware.py", line 52, in dispatch
response = await call_next(request)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 45, in call_next
task.result()
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/middleware/base.py", line 38, in coro
await self.app(scope, receive, send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
await self.app(scope, receive, send)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
response = await func(request)
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/fastapi/routing.py", line 201, in app
raw_response = await run_endpoint_function(
File "/home/codespace/workspace/dagpi-image/.venv/lib/python3.8/site-packages/fastapi/routing.py", line 148, in run_endpoint_function
return await dependant.call(**values)
File "/home/codespace/workspace/dagpi-image/app/routes/image_routes.py", line 107, in blur_image
img, image_format = await blur(byt)
File "/opt/python/3.8.6/lib/python3.8/multiprocessing/queues.py", line 239, in _feed
obj = _ForkingPickler.dumps(obj)
File "/opt/python/3.8.6/lib/python3.8/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function blur at 0x7f87524091f0>: it's not the same object as app.image.pil_manipulation.blur
Does someone know why this keeps happening?
I was told the objects have to be serializable, I believe BytesIO is the only in/out of the image. That should be serializable.
Decorators typically produce wrapped functions that aren't easy to pickle (serialize) because they contain hidden state. When dealing with multiprocessing, you should avoid decorators and send ordinary global functions to run_in_executor. For example, you could re-write your executor decorator into a utility function:
_pool = concurrent.futures.ProcessPoolExecutor()
async def exec_async(fn, *args):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(_pool, fn, *args)
Instead of decorating a function with executor, you can just await it using await exec_async(some_function, arg1, arg2, ...). Likewise, you can rewrite the pil decorator into another utility:
def pil(image, transform):
img = PILManip.pil_image(image)
if img.format == "GIF":
frames = []
for frame in ImageSequence.Iterator(img):
res_frame = transform(frame)
frames.append(res_frame)
return PILManip.pil_gif_save(frames), "gif"
elif img.format in ["PNG", "JPEG"]:
img = transform(img)
return PILManip.pil_image_save(img), "png"
else:
raise BadImage("Bad Format")
The implementation of blur now becomes an ordinary function which calls pil, and which can be safely passed to exec_async:
def blur(image):
def transform(frame):
frame = frame.convert("RGBA")
return frame.filter(ImageFilter.BLUR)
return pil(image, transform)
#router.get("/blur/", responses=normal_response)
async def blur_image(url: str):
byt = await Client.image_bytes(url)
img, image_format = await exec_async(blur, byt)
return Response(img.read(), media_type=f"image/{image_format}")
Note: the above code is untested.
ENV
PhantomJS2.1.1;Windows10;selenium-3.8.0;python3
CODE
browser = webdriver.PhantomJS() //throw a UnicodeDecodeError
Problem
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position
2: invalid start byte
Details
Traceback (most recent call last):
File "d:/devtools/phantomjs-2.1.1-windows/bin/IndustryLeaderSpider.py",
line 20, in
browser = webdriver.PhantomJS('D:/phantomjs.exe')
File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py",
line 58, in init
desired_capabilities=desired_capabilities)
File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 154, in init
self.start_session(desired_capabilities, browser_profile)
File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 309, in execute
response = self.command_executor.execute(driver_command, params)
File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py",
line 460, in execute
return self._request(command_info[0], url, body=data)
File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py",
line 522, in _request
resp = opener.open(request, timeout=self._timeout)
File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 544, in _open
'_open', req)
File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 806, in
meth(r, proxy, type))
File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 814, in proxy_open
if req.host and proxy_bypass(req.host):
File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 2739, in proxy_bypass
return proxy_bypass_registry(host)
File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 2706, in proxy_bypass_registry
fqdn = socket.getfqdn(rawHost)
File "D:\Continuum\Anaconda3\lib\socket.py", line 673, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 2: invalid start byte
Do someone have this problem? How to fix it?
Your main error seems to be :
File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 460, in execute return self._request(command_info[0], url, body=data) File "D:\Continuum\Anaconda3\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 522, in _request resp = opener.open(request, timeout=self._timeout) File "D:\Continuum\Anaconda3\lib\urllib\request.py", line 526, in open response = self._open(req, data)
Try the following steps :
Run CCleaner tool to wipe off all the OS chores.
Take a System Reboot.
While you initiate PhantomJS() provide the absolute path of the phantomjs binary as follows :
driver = webdriver.PhantomJS(executable_path=r'C:\path\to\phantomjs.exe')
Ensure that there is no unnecessary imports in your script. Example : urllib.request
Execute your Test.
Well I have this weird problem. Same script works on windows7 and on windows10 throws AttributeError. And I just can't figure out what is the difference.
This is py code:
import asyncio
from proxybroker import Broker
async def use(proxies):
while True:
proxy = await proxies.get()
if proxy is None:
break
elif 'SOCKS5' in proxy.types: # filter by type
print('Found SOCKS5 proxy: %s' % proxy)
else:
print('Found proxy: %s' % proxy)
async def find(proxies, loop):
broker = Broker(queue=proxies,
timeout=8,
attempts_conn=3,
max_concurrent_conn=200,
judges=['https://httpheader.net/', 'http://httpheader.net/'],
providers=['http://www.proxylists.net/', 'http://fineproxy.org/eng/'],
verify_ssl=False,
loop=loop)
# only anonymous & high levels of anonymity for http protocol and high for others:
types = [('HTTP', ('Anonymous', 'High')), 'HTTPS', 'SOCKS4', 'SOCKS5']
countries = ['US', 'GB', 'DE']
limit = 10
await broker.find(types=types, countries=countries, limit=limit)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
proxies = asyncio.Queue(loop=loop)
tasks = asyncio.gather(find(proxies, loop), use(proxies))
loop.run_until_complete(tasks)
And this is the error that it throws:
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\python.exe "C:/Users/draga/Desktop/Rts/PROXY/Proxy.py"
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\client.py:576: DeprecationWarning: Use async with instead
warnings.warn("Use async with instead", DeprecationWarning)
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\helpers.py:72: DeprecationWarning: ClientSession.close() is not coroutine
warnings.warn(self._msg, DeprecationWarning)
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\helpers.py:72: DeprecationWarning: ClientSession.close() is not coroutine
warnings.warn(self._msg, DeprecationWarning)
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\helpers.py:72: DeprecationWarning: ClientSession.close() is not coroutine
warnings.warn(self._msg, DeprecationWarning)
Traceback (most recent call last):
File "C:/Users/draga/Desktop/Rts/PROXY/Proxy.py", line 35, in <module>
loop.run_until_complete(tasks)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 466, in run_until_complete
return future.result()
File "C:/Users/draga/Desktop/Rts/PROXY/Proxy.py", line 29, in find
await broker.find(types=types, countries=countries, limit=limit)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\api.py", line 108, in find
await self._run(self._checker.check_judges(), action)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\api.py", line 114, in _run
await tasks
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\api.py", line 153, in _grab
proxies = await task
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\asyncio\tasks.py", line 452, in _wait_for_one
return f.result() # May raise f.exception().
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 68, in get_proxies
await self._pipe()
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 101, in _pipe
await self._find_on_page(self.url)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 117, in _find_on_page
page = await self.get(url, data=data, headers=headers, method=method)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 132, in get
page = await self._get(url, data=data, headers=headers, method=method)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 144, in _get
method, url, data=data, headers=headers) as resp:
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\client.py", line 637, in __aenter__
self._resp = yield from self._coro
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\client.py", line 231, in _request
conn = yield from self._connector.connect(req)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 378, in connect
proto = yield from self._create_connection(req)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 686, in _create_connection
_, proto = yield from self._create_direct_connection(req)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 697, in _create_direct_connection
hosts = yield from self._resolve_host(req.url.raw_host, req.port)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 666, in _resolve_host
self._cached_hosts.expired(key):
AttributeError: 'dict' object has no attribute 'expired'
Update: it seems the problem is in loop.run_until_complete(tasks) still dunno why.
ProxyBroker doesn't works after updating aiohttp to 2.1.0... You have to downgrade it to older version.