botasaurus
botasaurus copied to clipboard
when running on server getting error: HTTP server ListenAndServe: listen tcp: lookup tcp/▒H▒▒▒: Servname not supported for ai_socktype
When I executed the script locally it is running fine. but once I tried to run it in Ubuntu server getting the following error:
ubuntu@python:~/automated_scripts/scripts/stock$ python3 test.py
HTTP server ListenAndServe: listen tcp: lookup tcp/▒H▒▒▒: Servname not supported for ai_socktype
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.10/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
_queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/client.py", line 485, in execute_request
resp = self.server.post(
File "/home/ubuntu/.local/lib/python3.10/site-packages/geventhttpclient/client.py", line 277, in post
return self.request(METHOD_POST, request_uri, body=body, headers=headers)
File "/home/ubuntu/.local/lib/python3.10/site-packages/geventhttpclient/client.py", line 233, in request
sock = self._connection_pool.get_socket()
File "/home/ubuntu/.local/lib/python3.10/site-packages/geventhttpclient/connectionpool.py", line 161, in get_socket
return self._create_socket()
File "/home/ubuntu/.local/lib/python3.10/site-packages/geventhttpclient/connectionpool.py", line 126, in _create_socket
raise first_error
File "/home/ubuntu/.local/lib/python3.10/site-packages/geventhttpclient/connectionpool.py", line 113, in _create_socket
sock = self._connect_socket(sock, sock_info[-1])
File "/home/ubuntu/.local/lib/python3.10/site-packages/geventhttpclient/connectionpool.py", line 134, in _connect_socket
sock.connect(address)
File "/home/ubuntu/.local/lib/python3.10/site-packages/gevent/_socketcommon.py", line 590, in connect
self._internal_connect(address)
File "/home/ubuntu/.local/lib/python3.10/site-packages/gevent/_socketcommon.py", line 634, in _internal_connect
raise _SocketError(err, strerror(err))
ConnectionRefusedError: [Errno 111] Connection refused
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ubuntu/automated_scripts/scripts/stock/test.py", line 83, in <module>
res = get('https://google.com')
File "/home/ubuntu/automated_scripts/scripts/stock/test.py", line 80, in get
response = req.get(url)
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/request_class.py", line 61, in get
return reqs.get(url, **kwargs)
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/reqs.py", line 339, in get
return _get(url, *args, **add_redirects(kwargs, True))
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/reqs.py", line 248, in request
req.send()
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/reqs.py", line 135, in send
raise e
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/reqs.py", line 132, in send
self.response = self.session.request(self.method, self.url, **merged_kwargs)
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/session.py", line 192, in request
proc.send()
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/response.py", line 62, in send
self.response = self.execute_request()
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/response.py", line 74, in execute_request
raise e
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/response.py", line 67, in execute_request
resp = self.session.execute_request(
File "/home/ubuntu/.local/lib/python3.10/site-packages/botasaurus_requests/client.py", line 490, in execute_request
raise ClientException('Request failed') from e
botasaurus_requests.exceptions.ClientException: Request failed
ubuntu@python:~/automated_scripts/scripts/stock$
could you please help to understand if it is problem in my usage if the framework or it is really issue? I'm tried to use simple code:
from botasaurus.request import Request
def get(url):
req = Request()
response = req.get(url)
return response
res = get('https://google.com')
print(res)
or
from botasaurus.request import request, Request
@request(output=None, close_on_crash=True, max_retry=5)
def get(request: Request, url):
response = request.get(url)
res = get('https://google.com')
print(res)
the result is same error as mentioned above.
python version is 3.10
thanks in advance
I have the same problem in Ubuntu, so I had to work with other libraries.
Same issue for me too:
HTTP server ListenAndServe: listen tcp: lookup tcp/t cap: Servname not supported for ai_socktype
Ubuntu 20.04.6 LTS Python 3.8.10
@Chetan11-dev - could you please suggest how can we solve it?
After many hours of torment, I finally figured out what the error was and how to solve it.
The error lies in the library hrequests, which use GO. It can be found on this path: /site-packages/botasaurus_requests/bin/hrequests-cgo-2.1-linux-amd64.so Python passes a random listening port to this library, pre-encoding it in utf-8. However, the decoding fails at the output and we get exception. I solved this problem by simply changing the port to my own in hrequests.
//export GetOpenPort
func GetOpenPort() int {
return 40697
}
func main() {
/*
Start the HTTP server
*/
if len(os.Args) < 2 {
fmt.Println("Usage: <program-name> <port-number>")
os.Exit(1)
}
port := "40697" // port is passed as the first argument
fmt.Printf("Starting server at http://localhost:%s\n", "40697")
startServer(port)
}
func startServer(port string) {
srv = &http.Server{Addr: ":" + "40697"}
http.HandleFunc("/request", requestHandler)
http.HandleFunc("/multirequest", multiRequestHandler)
http.HandleFunc("/ping", pingHandler)
// start server
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// log the error if it's not ErrServerClosed, which we expect when calling srv.Close()
fmt.Printf("HTTP server ListenAndServe: %v\n", err)
}
}
//export StartServer
func StartServer(port string) {
// exposed function to start the server in a goroutine
go startServer("40697")
}
Then I had to struggle with the compilation, but in the end everything works fine. If you don't want to suffer, I'm attaching my compiled library. Just replacing it and everything fine.
https://github.com/domovenoc/modify-hrequests/blob/main/hrequests-cgo-2.1-linux-amd64.so