5
from os import system
system("ping www.twitter.com")
system("ping www.yahoo.com")
system("ping www.facebook.com")

I am in China, and Twitter and Facebook are banned here. I can open them in the browser using Clash for Windows software.

I have to download tweets from Twitter. So I need to ping the websites using Python to get tweets. I cannot ping the websites though.

How do I make my Python code use the Clash for Windows.

Output of the above code:

Pinging www.twitter.com [108.160.169.186] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 108.160.169.186:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

Pinging new-fp-shed.wg1.b.yahoo.com [180.222.102.201] with 32 bytes of data:
Reply from 180.222.102.201: bytes=32 time=258ms TTL=42
Reply from 180.222.102.201: bytes=32 time=229ms TTL=42
Reply from 180.222.102.201: bytes=32 time=230ms TTL=42
Request timed out.

Ping statistics for 180.222.102.201:
    Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
    Minimum = 229ms, Maximum = 258ms, Average = 239ms

Pinging www.facebook.com [69.63.184.14] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 69.63.184.14:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

OS: Windows 10 (updated to latest edition). Using PyCharm as my IDE.

Karol Zlot
  • 2,887
  • 2
  • 20
  • 37
Adnan Ali
  • 2,851
  • 5
  • 22
  • 39
  • 3
    Whether or not `ping` works is by and large unrelated to whether HTTP(s) works. Probably ignore `ping` and just figure out how to route DNS and HTTPS through your VPN. – tripleee Jun 05 '21 at 16:15
  • Yes, you are right. I try to do this. – Adnan Ali Jun 07 '21 at 03:44
  • Sounds like your default route is not being set for the vpn but the local interface. You need to fix your underlying network issue before you worry about your code. You give no details on the setup of the networking so im unable to say whats wrong. – user3788685 Jun 12 '21 at 13:44
  • I have mentioned the software I am using, its paid service. Internet is University internet but I also used my sim internet (via connecting phone USB tethering), the results mentioned are via USB tethering. – Adnan Ali Jun 16 '21 at 02:40

8 Answers8

1

You said in comment that you are using Clash, however Clash is not a VPN:

Clash - A rule-based tunnel in Go.

Features:

  • Local HTTP/HTTPS/SOCKS server with authentication support
  • VMess, Shadowsocks, Trojan, Snell protocol support for remote connections
  • Built-in DNS server that aims to minimize DNS pollution attack impact, supports DoH/DoT upstream and fake IP.
  • Rules based off domains, GEOIP, IP CIDR or ports to forward packets to different nodes
  • Remote groups allow users to implement powerful rules. Supports automatic fallback, load balancing or auto select node based off latency
  • Remote providers, allowing users to get node lists remotely instead of hardcoding in config
  • Netfilter TCP redirecting. Deploy Clash on your Internet gateway with iptables.
  • Comprehensive HTTP RESTful API controller

source: https://github.com/Dreamacro/clash

I'm not sure exactly how it works, my current understanding is that it allows you to use proxies.

Although it also has TUN mode as a "Premium Feature" (not sure what does it mean, I see no option to buy "Premium") which may work similarly to VPN, but I'm not sure:

Premium Features:

  • TUN mode on macOS, Linux and Windows. Doc
  • Match your tunnel by Script
  • Rule Provider

Clash for Windows which you try to use, is GUI for Clash.

Documentation is available, but it is only in Chinese:

https://github.com/Fndroid/clash-win-docs-new

screenshot:

Clash for Windows

I tried to use it, but I don't know how to configure it. It didn't work at all.


I see many possible solutions:

  1. You can switch to proper VPN. Paid VPNs are more recommended than free ones. Cost is typically 3-10$/month depending on offer. Alternatively you can try to setup OpenVPN on your own VPS, it may be a bit cheaper, but not necessarily.
  2. You can configure your script to use proxy. Libraries like requests support it: Proxies with Python 'Requests' module
  3. You can try to read Clash for Windows documentation to see if it is possible what you try to achieve. Maybe it is enough to turn on System Proxy as visible on screenshot above?
  4. You can try to configure Clash in TUN mode. In my opinion it may be more difficult than solutions 1 and 2. If you prefer this way, I suggest to read Clash documentation thoroughly: https://github.com/Dreamacro/clash/wiki/premium-core-features#tun-device

If you can afford it, I recommend solution 1 in most use-cases. I understand that you prefer free solution, however servers are not free, somebody needs to incur costs of running them (hardware, electricity etc.)

Karol Zlot
  • 2,887
  • 2
  • 20
  • 37
  • 1
    You need a YAML profile to use it. and it's paid. When you buy a profile, it has proxies then you can use it. just software does not work. the pity is, customer care also doesn't know how to solve the issue. – Adnan Ali Jun 16 '21 at 02:47
1

Here is the solution:

  1. go to https://www.wintun.net and download the latest release, copy the right wintun.dll into Clash home directory
  2. restart Clash for windows
  3. open clash dashboard, switch TUN Mode on

https://github.com/Dreamacro/clash/wiki/premium-core-features#windows

Jockio
  • 21
  • 5
  • what exactly it will do? i dont understand the working. – Adnan Ali Jan 14 '22 at 13:01
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30806793) – IgorZ Jan 16 '22 at 16:20
0

There is a variety of Python libraries that can help you. openpyn is one of them. Firstly, call this command in your terminal for setup:

sudo openpyn --init

After which all your Internet traffic can be redirected to a VPN server using the following command:

openpyn us

The command above is the default, to transfer all the traffic to the US. Other locations can be chosen, see the link above for more info.

As soon as your traffic is redirected, you are free to see banned sites as you did:

from os import system
system("ping www.twitter.com")
system("ping www.facebook.com")
Andrew Polukhin
  • 136
  • 1
  • 9
  • 1- this answer is related to open VPN (which i am not using). 2- I am not using Linux. I updated my question. – Adnan Ali Jun 07 '21 at 03:37
0

When you have VPN running and active then it should redirect all traffic via VPN server. If it is not redirecting all traffic, then maybe it was configured to redirect only webrowser traffic. Also it is possible that you aren't using true VPN, but a proxy.

Please share which VPN are you using, it will help us to help you.


You can just start VPN manually and then try executing your Python code.

Alternatively you can control when your VPN is running from Python. It requires different library for each VPN provider. Please tell us which VPN do you use.

This is working example for NordVPN using nordvpn_switcher package:


import time 
from nordvpn_switcher import initialize_VPN,rotate_VPN,terminate_VPN

initialize_VPN(save=1,area_input=['complete rotation'])

for i in range(1):
    rotate_VPN()

    from os import system
    system("ping www.twitter.com")
    system("ping www.yahoo.com")
    system("ping www.facebook.com")


    print('\nDo whatever you want here (e.g. pinging). Pausing for 10 seconds...\n')
    time.sleep(10)

terminate_VPN()

Karol Zlot
  • 2,887
  • 2
  • 20
  • 37
  • This is the VPN I am using https://github.com/Fndroid/clash_for_windows_pkg/releases and I am pretty sure its not Proxy. – Adnan Ali Jun 09 '21 at 12:10
  • @AdnanAli if you don't want to switch to another solution and prefer to stay with Clash, then I suggest to ask in github issues for this project: https://github.com/Fndroid/clash_for_windows_pkg/issues – Karol Zlot Jun 10 '21 at 13:47
0

Tor uses SOCKS 5 proxy server to give anonymity. In case you're using Tor, there are two ways to use SOCKS 5 proxy, one is to configure it at application level(browser, python code etc..), another to configure at Network Interface level. If you're using Tor, simply follow this answer, socks server is running at localhost:9050 by default - How to make python Requests work via socks proxy Since you haven't done anything and it's already working, I guess you're using tunnel-based VPN. In this case, it should work automatically. In your case, ping could be blocked by the VPN provider.

Ping uses ICMP protocol while HTTP/HTTPS uses TCP protocol in the Transport layer (in OSI model). They are very different things, and one working doesn't guarantee other working. In many cases, ping is blocked, by the server or other middlewares that don't support ICMP protocols. Most cloud providers don't support ICMP protocols in their networking components, for ex. Azure doesn't, for their Load Balancers. So, instead of trying ping, you should try the real http request. Following is sample code

import requests
r = requests.get("www.google.com")
TheJoker
  • 100
  • 8
0

You can use this code:

from ping3 import ping

p=ping("example.com")

print(p)
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
M Kh
  • 1
  • 1
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Suraj Rao Oct 24 '21 at 15:33
0

Here is an update for people facing the same problem.

There is an option in settings>System Proxy>Specify Protocol in Clash for Windows , I turned it on.

Before i was not able to run pip commands, after turning this on, i can do that. I hope it will be useful for someone. (This is not exact answer to the question but indeed it is useful for someone, i am sure)

enter image description here

Adnan Ali
  • 2,851
  • 5
  • 22
  • 39
-1

Let's try to isolate this problem if it's Python or network-related.

Does it work if you run ping in the shell directly?

ping twitter.com

It's possible that your VPN has a setting that blocks Pings from the internet. It should be one of the configurations in the Firewall.

maksim
  • 121
  • 8
  • My best guess is, its network related. because I can not ping via CMD or Python. but my ultimate goal is via Python. 2- VPN is already allowed in Firewall. if it is not, then how can I open Twitter in the browser. – Adnan Ali Jun 07 '21 at 03:34
  • Firewall can block only Ping requests. Check in the firewall if there is an option for allowing/disallowing (ICMP) packets https://en.wikipedia.org/wiki/Ping_(networking_utility). The browser is not using Ping to connect to the websites. Check if your python is able to resolve hostname to ip address (https://www.simplified.guide/python/hostname-to-ip). If it can do that, then it means that ping is not working because Firewall is blocking ping somehow. Also, I'm curious if you can `ping 127.0.0.1` or it also fails. – maksim Jun 07 '21 at 15:34