简体   繁体   中英

Problem sending a get request using sockets (PYTHON)

My Code:

import socket
request = b"GET / HTTP/1.1\nHost: api16-normal-c-useast1a.tiktokv.com\n\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("api16-normal-c-useast1a.tiktokv.com/aweme/v1/aweme/favorite/?max_cursor=0&count=0&ac=wifi&aid=1180&user_id=6986127446509143000", 80))
s.send(request)
result = s.recv(10000)
while (len(result) > 0):
    print(result)
    result = s.recv(10000)

My Error:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    s.connect(("api16-normal-c-useast1a.tiktokv.com/aweme/v1/aweme/favorite/?max_cursor=0&count=0&ac=wifi&aid=1180&user_id=6986127446509143000", 80))
socket.gaierror: [Errno 11001] getaddrinfo failed

Any help is appreciated thank you!.

An argument to connect should only contain a domain name, with the request path provided in the request itself. You should change you code to the following:

import socket
request = b"GET /aweme/v1/aweme/favorite/?max_cursor=0&count=0&ac=wifi&aid=1180&user_id=6986127446509143000 HTTP/1.1\nHost: api16-normal-c-useast1a.tiktokv.com\n\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("api16-normal-c-useast1a.tiktokv.com", 80))
s.send(request)
result = s.recv(10000)
while (len(result) > 0):
    print(result)
    result = s.recv(10000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM