FooSoft / anki-connect

Anki plugin to expose a remote API for creating flash cards.
https://foosoft.net/projects/anki-connect/
Other
1.92k stars 218 forks source link

Requests on Python + Windows are always around ~2s long #389

Closed Aquafina-water-bottle closed 1 year ago

Aquafina-water-bottle commented 1 year ago

Hey all,

I believe there's a strange problem where sending Anki-Connect requests on Python+Windows takes about 2 seconds. I tried using urllib.request and the requests library, but they both get the same result (time of around 2 seconds). I sent the code for urllib.request to someone else, and they were able to reproduce the issue. This issue appears to also be present when disabling Windows defender, and downgrading to Python 8 + Anki 2.1.49.

The strangest part is that I cannot reproduce this with any other combination, whether it is JavaScript on Windows, PowerShell on Windows, Python on Linux, curl on Linux, etc.

Example code:

import urllib.request
import time

start = time.time()
x = urllib.request.urlopen(urllib.request.Request("http://localhost:8765", {}))
end = time.time()
print(x, end-start)
import requests
import time

start = time.time()
x = requests.post("http://localhost:8765", "{}")
end = time.time()
print(x, end-start)

If anyone happens to know the source of the issue, a workaround to this, or even a simple lead on why this happens in the first place, please let me know!

Aquafina-water-bottle commented 1 year ago

Seems like this has to do with how localhost is resolved on Windows:

The main workaround is to use 127.0.0.1 instead of localhost.