Skip to content

Commit

Permalink
Force using IPv4 when the machine supports IPv6 networks
Browse files Browse the repository at this point in the history
Instagram seems to be ignoring connections coming from IPv6 networks, and
the scraper does not run when running on machines with IPv6 support. This
commit makes ensures that the scraper is using IPv4 connections.
  • Loading branch information
punchagan authored and rarcega committed Feb 7, 2020
1 parent 9a6fc01 commit 6a7ff3c
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions instagram_scraper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import os
import pickle
import re
import socket
import sys
import textwrap
import time
Expand All @@ -26,6 +27,7 @@
import threading
import concurrent.futures
import requests
import requests.packages.urllib3.util.connection as urllib3_connection
import tqdm

from instagram_scraper.constants import *
Expand All @@ -52,8 +54,14 @@ def write(self, x):
def flush(self):
return getattr(self.file, 'flush', lambda: None)()

def allowed_gai_family():
family = socket.AF_INET # force IPv4
return family

original_stdout, original_stderr = sys.stdout, sys.stderr
sys.stdout, sys.stderr = map(LockedStream, (sys.stdout, sys.stderr))
# Force using IPv4 connections, when the machine where this code runs uses IPv6
urllib3_connection.allowed_gai_family = allowed_gai_family

def threaded_input(prompt):
with input_lock:
Expand Down

0 comments on commit 6a7ff3c

Please sign in to comment.