Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

HTTP server ListenAndServe: listen tcp: lookup tcp/3/uti: Servname not supported for ai_socktype #63

Open
dmacoritto opened this issue Nov 7, 2024 · 3 comments

Comments

@dmacoritto
Copy link

dmacoritto commented Nov 7, 2024

Hello everyone,

I am having issues to use the hrequests package on my server.

When running

import hrequests

I get the following warning:

HTTP server ListenAndServe: listen tcp: lookup tcp/3/uti: Servname not supported for ai_socktype

Then when I run the follwing code (I just changed the website api url):

random_browser = random.choice(["chrome", "firefox"])
random_os = random.choice(["mac", "win"])
username = proxy_username
password = proxy_password
proxy = f"https://{username}:{password}@ch.smartproxy.com:29000"

# stealth request with fingerprint to bypass cloudflare
session = hrequests.Session(
    browser=random_browser,
    os=random_os,
    proxy=proxy,
)

resp = session.post(
    url="https://api.test.com/search/listings", json=post_payload
)

I get the following error

Traceback (most recent call last):
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/geventhttpclient/connectionpool.py", line 158, in get_socket
    return self._socket_queue.get(block=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "src/gevent/queue.py", line 335, in gevent._gevent_cqueue.Queue.get
  File "src/gevent/queue.py", line 350, in gevent._gevent_cqueue.Queue.get
  File "src/gevent/queue.py", line 319, in gevent._gevent_cqueue.Queue._Queue__get_or_peek
    '''Return the approximate size of the queue (not reliable!).'''
_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/hrequests/client.py", line 466, in execute_request
    resp = self.server.post(
           ^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/geventhttpclient/client.py", line 277, in post
    return self.request(METHOD_POST, request_uri, body=body, headers=headers)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/geventhttpclient/client.py", line 233, in request
    sock = self._connection_pool.get_socket()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/geventhttpclient/connectionpool.py", line 161, in get_socket
    return self._create_socket()
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/geventhttpclient/connectionpool.py", line 126, in _create_socket
    raise first_error
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/geventhttpclient/connectionpool.py", line 113, in _create_socket
    sock = self._connect_socket(sock, sock_info[-1])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/geventhttpclient/connectionpool.py", line 134, in _connect_socket
    sock.connect(address)
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/gevent/_socketcommon.py", line 590, in connect
    self._internal_connect(address)
  File "/opt/miniconda3/envs/scraper/lib/python3.11/site-packages/gevent/_socketcommon.py", line 634, in _internal_connect
    raise _SocketError(err, strerror(err))
ConnectionRefusedError: [Errno 111] Connection refused

When running this locally, everything goes fine. It was also fine on the server for a few days, but at some point I got this error and I am not able to understand what goes wrong.

Any help is appreciated.

@woodly0
Copy link

woodly0 commented Nov 20, 2024

Same issue here but with pipenv. On import I get the warning:

HTTP server ListenAndServe: listen tcp: lookup tcp/@…�Ñd: unknown port

Weirdly, it doesn't appear on every run. Only sometimes. What does that mean??

@alexlokhov
Copy link

Having the same issue.
Doesn't appear on every run, only sporadically. Happens with hrequests 0.9.1 and 0.9.2 on ubuntu Linux 5.4.0-200-generic #220-Ubuntu SMP Fri Sep 27 13:19:16 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Getting this error after 'import hrequests' (always some random characters after tcp/):
HTTP server ListenAndServe: listen tcp: lookup tcp/`▒: invalid argument

Subsequent requests throw an error
ConnectionRefusedError: [Errno 111] Connection refused

@alexlokhov
Copy link

So I have been trying to solve the problem myself.
hrequests uses a server written in Go. As far as I can tell, the correct port number is passed to the server from python and I struggle to investigate the problem further. I tried different versions of python, updated all the Go libraries, but still get the same error, no matter what.

As a temporary workaround I managed to make it work from a Docker container (consistently, as far as I can tell)

cat Dockerfile
# Use an official Python image with the desired version
FROM python:3.11-slim

# Install necessary system dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip setuptools wheel \
    && pip install --no-cache-dir hrequests

# Set the default command to run your script
CMD ["python", "scrape.py"]

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants